rfc:fallback-to-root-scope-deprecation

This is an old revision of the document!


PHP RFC - Deprecation of fallback to root scope

Introduction

Fallback to root scope was implemented to facilitate the transition of existing classes to namespaced ones.

<1 Levi thinks this should be removed>

It made possible to easily convert existing code to their namespaced equivalent by simply replacing class Vendor_Lib_Type{} with namespace Vendor\Lib; class Type{}. Their users then just needed (pretty much) to replace _ with \ to match them. No other change was required for the upgrade, as PHP would have automatically redirected any unqualified function call and constant access to the root scope.

While this helped the transition to namespaces a lot, it created several problems. </1> It forces PHP to constantly re-check what a symbol actually is every time it is accessed (https://3v4l.org/C0ZLq); it prevents PHP to implement function and constant autoloading in a sensible manner and consequently denies all the ramifications that would be possible if this feature existed. More and more people would use functions these days, but the prospect of having to maintain a list of require()s nauseate them.

Proposal

This RFC proposes to deprecate the fallback to root scope, by emitting a Notice, e.g.:

Undefined function \My\NS\strlen(), assumed \strlen()
Undefined constant \My\NS\PHP_VERSION, assumed \PHP_VERSION

It also proposes, considered the entity of the change, that the “fallback to root scope” feature must be removed through the RFC process and thus only when the community think is appropriate doing so (RFC author's prediction: hopefully in PHP 8 but more likely 9).

Impact to users, before and after the removal

PHPStorm

PHPStorm is a very valuable tool and it is worth mentioning it as it's the de-facto standard PHP IDE. Thanks to JetBrains and the PHP community, PHPStorm 2017 will have a good support of function and constant completion.

Depending on a per-project setting, users will have either \strlen() or use function strlen; automatically added as soon they type in strlen (WI-27425, WI-34446).

The PHPStorm's PHP Inspections extension (WI-34858) will also show a warning when unqualified symbols are used.

As such, users will be able to avoid the Notice without changing their habits.

Roave's FQN replacer

Another valuable tool is Roave's FunctionFQNReplacer; allows us to automatically replace unqualified identifiers with their qualified counterpart in seconds.

Fixing broken code after the eventual removal

The “fallback to root scope” feature should ideally be removed in conjunction of the introduction of “function and constant autoloading”; broken code may take advantage of that and be (temporarily) fixed by appending an autoloader that aliases the root scope's symbol in the current namespace, e.g.:

register_autoloader(
    AUTOLOAD_FUNCTION | AUTOLOAD_CONST,
    function(string $QN, int $type){
 
        $start = strrpos($QN, "\\");
        if($start === false){ return; }
 
        // e.g. "My\Full\NS"
        $sourceNS = substr($QN, 0, $start);
 
        // e.g. "strlen"
        $rootSymbol = substr($QN, $start + 1);
 
        if(
            ($type & AUTOLOAD_FUNCTION) &&
            function_exists($rootSymbol)
        ){
            eval('
                namespace ' . $sourceNS . ';
                function ' . $rootSymbol . '(...$args){
                    return \\' . $rootSymbol . '(...$args);
                }
            ');
        }elseif(
            ($type & AUTOLOAD_CONST) &&
            defined($rootSymbol)
        ){
            eval('
                namespace ' . $sourceNS . ';
                const ' . $rootSymbol . ' = \\' . $rootSymbol . ';
            ');
        }
    }
);

This would provide a decent enough solution (as what it does is basically emulating in userland what PHP currently does in core) for nearly the totality of the existing code, thus covering those users that failed to update their code in time.

Backward Incompatible Changes

None (the Notice can be silenced if needed).

Proposed PHP Version:

7.3

Voting

2/3 majority will be required.

References

@TODO link discussion

rfc/fallback-to-root-scope-deprecation.1502022848.txt.gz · Last modified: 2017/09/22 13:28 (external edit)