WesNetmo

WesNetmo

levim

Levi Morrison

Ocramius

Ocramius , from Marco Pivetta Software Development
(user votes do not necessarily reflect their company's views)

Contents

PHP RFC - Deprecation of fallback to root scope

Introduction

Fallback to global scope allows namespaces to access in an unqualified manner symbols actually residing in the root namespace.

namespace Bar;
strlen();
// first tries to call \Bar\strlen()
// if not found, fallbacks to \strlen()

This feature causes more harm than good, since it prevents PHP from implementing in a sensible manner long-requested features like function autoloading. Additionally, it causes PHP to behave weirdly and inefficiently (https://3v4l.org/C0ZLq).

Proposal

This RFC proposes to deprecate the fallback to root scope, by emitting a deprecation 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 and only alongside the introduction of the “function and constant autoloading” feature.

In fact, if the feature is removed and autoloading of functions and constants is introduced at the same time, authors that failed to update their code can easily shim it using just few lines of code:

// Fallback to global scope shim
// This code simply aliases \strlen to \Current\NS\strlen
// Note: this is just a non-binding PoC
autoload_register(AUTOLOAD_FUNCTION | AUTOLOAD_CONST, function(string $namespaced, int $type){
    // Assumes $namespaced is NOT prepended with \
    // Assumes $namespaced is NOT already loaded
 
    // Find last occurrence of \
    $offset = \strrpos($namespaced, "\\");
 
    // Return if none; this is only active for namespaced symbols
    if($offset === false){ return; }
 
    // Fallback symbol's name:
    $fallback = \substr($namespaced, $offset + 1);
 
    // Alias the function
    if($type & \AUTOLOAD_FUNCTION && \function_exists($fallback /*, false [1] */)){
        \function_alias($fallback, $namespaced); // May be introduced too, works like class_alias
    }
 
    // Alias the constant
    if($type & \AUTOLOAD_CONST && \defined($fallback /*, false [1] */)){
        \define($namespaced, \constant($fallback));
    }
});
 
// [1] = may not trigger autoloading from an autoloader

However, if this RFC passes, authors should try to avoid the Notice by writing `\strlen()` or `use function {strlen, strpos};` in their code, which are plenty of solutions covering any code style.

Migration Tools

Backward Incompatible Changes

None (the Notice can be silenced if needed).

Proposed PHP Version:

7.3

Voting

2/3 majority will be required.

References