rfc:fallback-to-root-scope-deprecation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:fallback-to-root-scope-deprecation [2017/03/06 02:49] wesnetmorfc:fallback-to-root-scope-deprecation [2018/02/03 16:26] (current) wesnetmo
Line 1: Line 1:
-====== PHP RFC - Deprecation of fallback to root scope======+====== PHP RFC - Deprecation of fallback to root scope ======
  
   * Version: 0.1   * Version: 0.1
   * Date: 2017-03-05   * Date: 2017-03-05
-  * Author: WesNetmo@Twitter, Levi Morrison, Marco Pivetta (Ocramius)+  * Author: WesNetmo, Levi Morrison, Ocramius
   * Status: Under Discussion   * Status: Under Discussion
   * First Published at: https://wiki.php.net/rfc/fallback-to-root-scope-deprecation   * First Published at: https://wiki.php.net/rfc/fallback-to-root-scope-deprecation
  
-===== Introduction=====+===== Introduction =====
  
-Fallback to root scope was implemented to facilitate the transition of existing classes to namespaced ones.+Fallback to global scope allows namespaces to access in an unqualified manner symbols actually residing in the root namespace.
  
-**<1 Levi thinks this should be removed>**+<code php> 
 +namespace Bar; 
 +strlen(); 
 +// first tries to call \Bar\strlen() 
 +// if not found, fallbacks to \strlen() 
 +</code>
  
-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 themNo other change was required for the upgradeas PHP would have automatically redirected any unqualified function call and constant access to the root scope.+This feature causes more harm than good, since it prevents PHP from implementing **in a sensible manner** long-requested features like function autoloadingAdditionallyit causes PHP to behave weirdly and inefficiently (https://3v4l.org/C0ZLq).
  
-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 =====
  
-@TODO more reasoning? +This RFC proposes to deprecate the fallback to root scope, by emitting a deprecation notice, e.g.:
- +
-===== Proposal: ===== +
- +
-This RFC proposes to deprecate the fallback to root scope, by emitting a ''Notice'', e.g.:+
  
 <code> <code>
Line 28: Line 29:
 </code> </code>
  
-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).+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**.
  
-===== Impact to usersbefore and after the removal=====+In factif 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:
  
-==== PHPStorm====+<code php> 
 +// Fallback to global scope shim 
 +// This code simply aliases \strlen to \Current\NS\strlen 
 +// Notethis 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
  
-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.+    // Find last occurrence of 
 +    $offset = \strrpos($namespaced, "\\");
  
-Depending on a per-project setting, users will have either ''\strlen()'' or ''use function strlen;'' automatically added as soon they type in ''strlen'' ([[https://youtrack.jetbrains.com/issue/WI-27425|WI-27425]], [[https://youtrack.jetbrains.com/issue/WI-34446|WI-34446]]).+    // Return if none; this is only active for namespaced symbols 
 +    if($offset === false){ return}
  
-The PHPStorm'PHP Inspections extension ([[https://youtrack.jetbrains.com/issue/WI-34858|WI-34858]]will also show a warning when unqualified symbols are used.+    // Fallback symbol'name: 
 +    $fallback = \substr($namespaced, $offset + 1);
  
-As such, **users will be able to avoid the ''Notice'' without changing their habits**.+    // Alias the function 
 +    if($type & \AUTOLOAD_FUNCTION && \function_exists($fallback /*, false [1] */)){ 
 +        \function_alias($fallback, $namespaced); // May be introduced too, works like class_alias 
 +    }
  
-==== Roave's FQN replacer: ====+    // Alias the constant 
 +    if($type & \AUTOLOAD_CONST && \defined($fallback /*, false [1] */)){ 
 +        \define($namespaced, \constant($fallback)); 
 +    } 
 +});
  
-Another valuable tool is [[https://github.com/Roave/FunctionFQNReplacer|Roave's FunctionFQNReplacer]]; allows us to automatically replace unqualified identifiers with their qualified counterpart in seconds. +// [1= may not trigger autoloading from an autoloader 
- +</code>
-==== 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.:+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**.
  
-<code php> +===== Migration Tools =====
-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 . '; +
-            '); +
-        } +
-    } +
-); +
-</code>+
  
-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.+  * [[https://github.com/squizlabs/PHP_CodeSniffer|PHP-CS]] 
 +  * [[https://github.com/FriendsOfPHP/PHP-CS-Fixer|PHP-CS-FIXER]] 
 +  * [[https://www.jetbrains.com/phpstorm/|PHPStorm]] 
 +  * More coming soon...
  
-===== Backward Incompatible Changes=====+===== Backward Incompatible Changes =====
  
 None (the ''Notice'' can be silenced if needed). None (the ''Notice'' can be silenced if needed).
Line 95: Line 79:
 ===== Proposed PHP Version: ===== ===== Proposed PHP Version: =====
  
-7.2+7.3
  
-===== Voting=====+===== Voting =====
  
 2/3 majority will be required. 2/3 majority will be required.
  
-===== References=====+===== References =====
  
-@TODO link discussion+- [[https://externals.io/message/101745|Discussion on externals]]
rfc/fallback-to-root-scope-deprecation.1488768559.txt.gz · Last modified: 2017/09/22 13:28 (external edit)