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 revisionLast revisionBoth sides next revision | ||
rfc:fallback-to-root-scope-deprecation [2017/03/05 13:39] – wesnetmo | rfc:fallback-to-root-scope-deprecation [2018/02/03 16:20] – 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: Levi Morrison, | + | * Author: |
* Status: Under Discussion | * Status: Under Discussion | ||
* First Published at: https:// | * First Published at: https:// | ||
- | ===== Introduction: ===== | + | ===== Introduction ===== |
- | Fallback to root scope was implemented | + | Fallback to global |
- | **<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 '' | + | 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 |
- | While this helped the transition to namespaces a lot, it created several problems. **</ | + | ===== 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 '' | + | |
< | < | ||
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 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 | + | |
- | Since this feature will be removed in conjunction with the introduction of "function | + | 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 ===== |
None (the '' | None (the '' | ||
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:// |
rfc/fallback-to-root-scope-deprecation.txt · Last modified: 2018/02/03 16:26 by wesnetmo