Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revisit global namespace resolution #16

Open
colinodell opened this issue Aug 10, 2021 · 1 comment
Open

Revisit global namespace resolution #16

colinodell opened this issue Aug 10, 2021 · 1 comment

Comments

@colinodell
Copy link
Member

Currently this standard requires that all references to things in the global namespace be prefixed with a \ wherever they are used:

$now = new \DateTimeImmutable();

$json = \json_encode($foo, \JSON_THROW_ON_ERROR);

Alternatively, we could remove the \s and require use statements for all of these:

use DateTimeImmutable;
use json_encode;
use JSON_THROW_ON_ERROR;

$now = new DateTimeImmutable();

$json = json_encode($foo, JSON_THROW_ON_ERROR);

Both of these approaches provide the same performance benefit:

Inside a namespace, when PHP encounters an unqualified Name in a class name, function or constant context, it resolves these with different priorities. Class names always resolve to the current namespace name. Thus to access internal or non-namespaced user classes, one must refer to them with their fully qualified Name.
...
For functions and constants, PHP will fall back to global functions or constants if a namespaced function or constant does not exist.

The question is whether we should continue using inline \s or if we should instead switch to use statements.

Personally, I prefer the inline \s as I feel it's easier to remember to include those versus maintaining the list of use statements at the top of the file (or needing to always run phpcbf after making changes). But I'm open to other opinions here.

@colinodell
Copy link
Member Author

Relevant Twitter poll: https://twitter.com/colinodell/status/1423313699675246594

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant