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
Next revisionBoth sides next revision
rfc:fallback-to-root-scope-deprecation [2017/08/06 12:34] wesnetmorfc:fallback-to-root-scope-deprecation [2018/02/03 12:30] wesnetmo
Line 3: Line 3:
   * 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
Line 9: Line 9:
 ===== 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; 
-It made possible to easily convert existing code to their namespaced equivalent by simply replacing ''class Vendor_Lib_Type{}'' with ''namespace Vendor\Libclass Type{}''. Their users then just needed (pretty much) to replace ''_'' with ''\'' to match them. No other change was required for the upgradeas PHP would have automatically redirected any unqualified function call and constant access to the root scope.+strlen()
 +// first tries to call \Bar\strlen() 
 +// if not foundfallbacks to \strlen() 
 +</code>
  
-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.+This feature causes more harm than goodsince 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 ===== ===== Proposal =====
  
-This RFC proposes to deprecate the fallback to root scope, by emitting a ''Notice'', e.g.:+This RFC proposes to deprecate the fallback to root scope, by emitting a deprecation notice, e.g.:
  
 <code> <code>
Line 26: 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 timeauthors that failed to update their code can easily shim it using just few lines of code:
- +
-==== PHPStorm ==== +
- +
-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 communityPHPStorm 2017 will have a good support of function and constant completion. +
- +
-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]]). +
- +
-The PHPStorm's PHP Inspections extension ([[https://youtrack.jetbrains.com/issue/WI-34858|WI-34858]]) will also show a warning when unqualified symbols are used. +
- +
-As such, **users will be able to avoid the ''Notice'' without changing their habits**. +
- +
-==== Roave's FQN replacer ==== +
- +
-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. +
- +
-==== 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.:+
  
 <code php> <code php>
 +// Fallback to global scope shim
 +// This code simply copies \strlen to \Current\NS\strlen
 register_autoloader( register_autoloader(
     AUTOLOAD_FUNCTION | AUTOLOAD_CONST,     AUTOLOAD_FUNCTION | AUTOLOAD_CONST,
Line 85: Line 72:
 </code> </code>
  
-This would provide a decent enough solution (as what it does is basically emulating in userland what PHP currently does in corefor nearly the totality of the existing code, thus covering those users that failed to update their code in time.+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 ===== 
 + 
 +  * [[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 =====
Line 101: Line 95:
 ===== References ===== ===== References =====
  
-@TODO link discussion+- [[https://externals.io/message/101745|Discussion on externals]]
rfc/fallback-to-root-scope-deprecation.txt · Last modified: 2018/02/03 16:26 by wesnetmo