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 [2018/02/03 08:32] wesnetmorfc:fallback-to-root-scope-deprecation [2018/02/03 16:26] (current) wesnetmo
Line 22: Line 22:
 ===== Proposal ===== ===== Proposal =====
  
-This RFC proposes to deprecate the fallback to root scope, by emitting a ''Notice'' (''E_NOTICE'' or ''E_STRICT''), e.g.:+This RFC proposes to deprecate the fallback to root scope, by emitting a deprecation notice, e.g.:
  
 <code> <code>
Line 29: 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** and **only alongside the introduction the "function and constant autoloading" feature**.+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: 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:
Line 35: Line 35:
 <code php> <code php>
 // Fallback to global scope shim // Fallback to global scope shim
-// This code simply copies \strlen to \Current\NS\strlen +// This code simply aliases \strlen to \Current\NS\strlen 
-register_autoloader( +// Note: this is just a non-binding PoC 
-    AUTOLOAD_FUNCTION | AUTOLOAD_CONST, +autoload_register(AUTOLOAD_FUNCTION | AUTOLOAD_CONST, function(string $namespaced, int $type){ 
-    function(string $QN, int $type){ +    // Assumes $namespaced is NOT prepended with \ 
-         +    // Assumes $namespaced is NOT already loaded 
-        $start = strrpos($QN, "\\"); + 
-        if($start === false){ return; } +    // Find last occurrence of \ 
-         +    $offset \strrpos($namespaced, "\\"); 
-        // e.g. "My\Full\NS" + 
-        $sourceNS = substr($QN, 0, $start); +    // Return if none; this is only active for namespaced symbols 
-         +    if($offset === false){ return; } 
-        // e.g. "strlen" + 
-        $rootSymbol = substr($QN, $start + 1); +    // Fallback symbol's name: 
-         +    $fallback \substr($namespaced, $offset + 1); 
-        if+ 
-            ($type & AUTOLOAD_FUNCTION&& +    // Alias the function 
-            function_exists($rootSymbol) +    if($type & \AUTOLOAD_FUNCTION && \function_exists($fallback /*, false [1] */)){ 
-        ){ +        \function_alias($fallback, $namespaced); // May be introduced too, works like class_alias
-            eval(' +
-                namespace ' . $sourceNS . '; +
-                function ' . $rootSymbol . '(...$args){ +
-                    return \\' . $rootSymbol . '(...$args); +
-                } +
-            '); +
-        }elseif( +
-            ($type & AUTOLOAD_CONST&& +
-            defined($rootSymbol) +
-        ){ +
-            eval(' +
-                namespace ' . $sourceNS . '; +
-                const ' . $rootSymbol . ' = \\' . $rootSymbol . '; +
-            '); +
-        }+
     }     }
-);+ 
 +    // Alias the constant 
 +    if($type & \AUTOLOAD_CONST && \defined($fallback /*, false [1] */)){ 
 +        \define($namespaced, \constant($fallback)); 
 +    } 
 +}); 
 + 
 +// [1] = may not trigger autoloading from an autoloader
 </code> </code>
  
Line 76: Line 68:
 ===== Migration Tools ===== ===== Migration Tools =====
  
-[[https://github.com/squizlabs/PHP_CodeSniffer|PHP-CS]] +  * [[https://github.com/squizlabs/PHP_CodeSniffer|PHP-CS]] 
-[[https://github.com/FriendsOfPHP/PHP-CS-Fixer|PHP-CS-FIXER]] +  [[https://github.com/FriendsOfPHP/PHP-CS-Fixer|PHP-CS-FIXER]] 
-[[https://www.jetbrains.com/phpstorm/|PHPStorm]] +  [[https://www.jetbrains.com/phpstorm/|PHPStorm]] 
-More coming soon...+  More coming soon...
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/fallback-to-root-scope-deprecation.1517646748.txt.gz · Last modified: 2018/02/03 08:32 by wesnetmo