[PR][php-src][#1149][master] - Make use not clash and crash if the class is already defined
| From: | noreply@php.net | Date: | Mon, 09 Mar 2015 00:30:44 +0000 |
| Subject: | [PR][php-src][#1149][master] - Make use not clash and crash if the class is already defined | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-2672@lists.php.net to get a copy of this message | ||
You can view the Pull Request on github:
https://github.com/php/php-src/pull/1149
Pull Request Description:
Currently, use will crash with an error if there is already a symbol with the same name defined.
namespace Foo {
class Bar {}
}
namespace Foo {
use Biz\Bar;
}
That will generate a fatal error that "bar" already exists in scope:
> Fatal error: Cannot use Biz\Bar as Bar because the name is already in use.
This is because
use does a symbol table lookup to see if there is a collision with any
declared class.
However, this is a problematic check since inclusion order matters. If the first block is executed
after the second one (different files) this will result in code working. If for some reason the
inclusion order changes, the code will fail.
Additionally, opcache nulls out the symbol tables prior to compilation. So with opcache enabled, the
code (assuming it lives in separate files) will never error, because the symbol table lookups will
always fail.
This results in the check being inconsistent at best. It means that the same conceptual code will
error if run one way, but not if run several others.
Therefore, I am proposing that we simply remove the error checks all together as the code is
perfectly easy to understand and 100% predictable.
This has the side effect that the following code becomes possible:
<?php
class Test {}
use Bar\Test;
var_dump(new Test); // class(Bar\Test)