Skip to content

Conversation

@ircmaxell
Copy link
Contributor

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)

@ghost
Copy link

ghost commented Mar 7, 2015

Can one of the admins verify this patch?

@smalyshev smalyshev added the RFC label Mar 9, 2015
@smalyshev
Copy link
Contributor

This would probably require an RFC as this is changing name resolution rules. I'm not sure allowing to hide classes with use declarations is a good idea, but maybe there are arguments for it or cases where it is useful.

@hikari-no-yume
Copy link
Contributor

I'd prefer the more intuitive approach of making use function foo; function foo() { ... }; simply produce an error.

@neclimdul
Copy link

Does producing an error address the include order problem? Its easy, especially with so many things being auto-loaded these days, for include order to be inconsistent and unpredictable.

Also as noted in the summary, with opcaches being the norm these days this change could break "working" code as well.

@nikic
Copy link
Member

nikic commented Feb 14, 2017

As of PHP 7.0.15(?) conflicts between imports and declared symbols are only forbidden if they occur within a single file, so the issue with the include order, as well as opcache interaction, is resolved. See also bug #66773.

As the immediate issue this was addressing has been resolved, and removing the check altogether appears to be controversial (and, as such, would need some discussion on internals), I'm closing this PR.

@nikic nikic closed this Feb 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants