Skip to content

Revisit global namespace resolution #16

Open
@colinodell

Description

@colinodell

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions