rfc:fallback-to-root-scope-deprecation
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:fallback-to-root-scope-deprecation [2017/03/06 02:59] – wesnetmo | rfc:fallback-to-root-scope-deprecation [2018/02/03 16:26] (current) – wesnetmo | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Version: 0.1 | * Version: 0.1 | ||
* Date: 2017-03-05 | * Date: 2017-03-05 | ||
- | * Author: WesNetmo@Twitter, Levi Morrison, | + | * Author: WesNetmo, Levi Morrison, Ocramius |
* Status: Under Discussion | * Status: Under Discussion | ||
* First Published at: https:// | * First Published at: https:// | ||
Line 9: | Line 9: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Fallback to root scope was implemented | + | Fallback to global |
- | **<1 Levi thinks this should be removed>** | + | <code php> |
- | + | namespace | |
- | It made possible to easily convert existing code to their namespaced equivalent by simply replacing '' | + | strlen(); |
- | + | // first tries to call \Bar\strlen() | |
- | 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 | + | // if not found, fallbacks |
+ | </ | ||
- | @TODO more reasoning? | + | This feature causes |
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC proposes to deprecate the fallback to root scope, by emitting a '' | + | This RFC proposes to deprecate the fallback to root scope, by emitting a deprecation notice, e.g.: |
< | < | ||
Line 28: | Line 29: | ||
</ | </ | ||
- | It also proposes, considered the entity of the change, that the " | + | It also proposes, considered the entity of the change, that the " |
- | ===== Impact to users, before | + | In fact, if the feature is removed |
- | ==== PHPStorm ==== | + | <code php> |
+ | // 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, | ||
+ | // 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 | + | // Find last occurrence |
+ | $offset = \strrpos($namespaced, | ||
- | Depending on a per-project setting, users will have either '' | + | // Return if none; this is only active for namespaced symbols |
+ | if($offset === false){ return; } | ||
- | The PHPStorm' | + | // Fallback symbol' |
+ | $fallback = \substr($namespaced, | ||
- | As such, **users will be able to avoid the '' | + | // Alias the function |
+ | if($type & \AUTOLOAD_FUNCTION && \function_exists($fallback /*, false [1] */)){ | ||
+ | \function_alias($fallback, | ||
+ | } | ||
- | ==== Roave' | + | // Alias the constant |
+ | if($type & \AUTOLOAD_CONST && \defined($fallback /*, false [1] */)){ | ||
+ | \define($namespaced, | ||
+ | } | ||
+ | }); | ||
- | Another valuable tool is [[https://github.com/ | + | // [1] = may not trigger autoloading from an autoloader |
- | + | </code> | |
- | ==== Fixing broken | + | |
- | The " | + | However, if this RFC passes, authors should try to avoid the Notice by writing `\strlen()` or `use function |
- | <code php> | + | ===== Migration Tools ===== |
- | register_autoloader( | + | |
- | AUTOLOAD_FUNCTION | AUTOLOAD_CONST, | + | |
- | function(string $QN, int $type){ | + | |
- | + | ||
- | $start | + | |
- | if($start | + | |
- | + | ||
- | // e.g. " | + | |
- | $sourceNS | + | |
- | + | ||
- | // e.g. " | + | |
- | $rootSymbol | + | |
- | + | ||
- | if( | + | |
- | ($type & AUTOLOAD_FUNCTION) && | + | |
- | function_exists($rootSymbol) | + | |
- | ){ | + | |
- | eval(' | + | |
- | namespace ' . $sourceNS . '; | + | |
- | function ' . $rootSymbol . ' | + | |
- | return \\' . $rootSymbol . ' | + | |
- | } | + | |
- | '); | + | |
- | }elseif( | + | |
- | ($type & AUTOLOAD_CONST) && | + | |
- | defined($rootSymbol) | + | |
- | ){ | + | |
- | eval(' | + | |
- | namespace ' . $sourceNS . '; | + | |
- | const ' . $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. | + | * [[https:// |
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * More coming soon... | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 95: | Line 79: | ||
===== Proposed PHP Version: ===== | ===== Proposed PHP Version: ===== | ||
- | 7.2 | + | 7.3 |
===== Voting ===== | ===== Voting ===== | ||
Line 103: | Line 87: | ||
===== References ===== | ===== References ===== | ||
- | @TODO link discussion | + | - [[https:// |
rfc/fallback-to-root-scope-deprecation.1488769169.txt.gz · Last modified: 2017/09/22 13:28 (external edit)