rfc:core-autoloading

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:core-autoloading [2023/04/03 13:03] – created girgiasrfc:core-autoloading [2023/04/23 14:24] (current) danack
Line 3: Line 3:
   * Version: 0.1   * Version: 0.1
   * Date: 2023-04-03   * Date: 2023-04-03
-  * Author: George Peter Banyard, <girgias@php.net> +  * Author: George Peter Banyard, <girgias@php.net>, Dan Ackroyd, <danack@basereality.com>  
-  * Status: Draft+  * Status: Under Discussion
   * Target Version: PHP 8.3   * Target Version: PHP 8.3
   * Implementation: [[https://github.com/php/php-src/pull/8294]]   * Implementation: [[https://github.com/php/php-src/pull/8294]]
Line 16: Line 16:
 The need for such a feature seems very clear as users will create "helper" classes with static methods to take advantage of autoloading via the class autoloading mechanism. The need for such a feature seems very clear as users will create "helper" classes with static methods to take advantage of autoloading via the class autoloading mechanism.
  
-A previous [[draft RFC for function autoloading|https://wiki.php.net/rfc/function_autoloading]] did not proceed to a vote, due to concerns on a performance hit related to global functions.+A previous [[https://wiki.php.net/rfc/function_autoloading|draft RFC for function autoloading]] did not proceed to a vote, due to concerns on a performance hit related to global functions.
 This RFC has avoided that performance problem. This RFC has avoided that performance problem.
  
Line 94: Line 94:
 </PHP> </PHP>
  
-If a function named ''strlen'' exists in the namespace ''bar'' PHP would use that, otherwise PHP would fallback to the <php>strlen()</php> function in the global namespace. Note, the <php>use function strlen;</php> syntax was introduced in PHP 5.6.+If a function named ''strlen'' exists in the namespace ''bar'' PHP would use that, otherwise PHP would 'fallbackto the <php>strlen()</php> function in the global namespace. Note, the <php>use function strlen;</php> syntax was introduced in PHP 5.6.
  
 This RFC preserves that fallback behaviour. This RFC preserves that fallback behaviour.
Line 100: Line 100:
 When code tries to call a function that doesn't currently exist in the current namespace, the function autoloader mechanism will call the registered function autoloaders once with the fully namespaced function name. When code tries to call a function that doesn't currently exist in the current namespace, the function autoloader mechanism will call the registered function autoloaders once with the fully namespaced function name.
  
-If an appropriately named function is not loaded during that call, the PHP engine will 'fallbackthat function to the global namespace.+If an appropriately named function is not loaded during that call, the PHP engine will fallback that function to the global namespace.
 If a function already exists with the global namespace name, that will be used, otherwise the function autoloader mechanism will call the registered function autoloaders once with the global function name. If a function already exists with the global namespace name, that will be used, otherwise the function autoloader mechanism will call the registered function autoloaders once with the global function name.
  
-After a function autoload attempt succeeds, the function will be 'pinned' to the function that was resolved on the first successful autoload attempt, and no more function autoload calls will be generated.+After a function in a namespace is resolved (i.e. a function is located in the current namespace either because it already exists, or a function autoload call loads it, or the global fallback occurs), the function will be 'pinned' to that function entry, and no more function autoload calls will be generated for that function in that namespace.
  
 This is possibly easier to understand through code: This is possibly easier to understand through code:
Line 185: Line 185:
 The global fallback is not invoked for functions with absolute names. Absolute function names happen: The global fallback is not invoked for functions with absolute names. Absolute function names happen:
  
-* In a PHP file that has an alias/import of a function, when that file is compiled, an absolute name is used for the function name. +  * In a PHP file that has an alias/import of a function, when that file is compiled, an absolute name is used for the function name. 
-* Functions that are directly invoked with an absolute name e.g. <php>\strlen("bar");</php>+  * Functions that are directly invoked with an absolute name e.g. <php>\strlen("bar");</php>
  
 For functions that are not invoked with an absolute function name, the function autoloader will be called once per function name per namespace. After that, the function will be pinned to the function that was resolved. For functions that are not invoked with an absolute function name, the function autoloader will be called once per function name per namespace. After that, the function will be pinned to the function that was resolved.
Line 249: Line 249:
  
 Passing <php>spl_autoload_call()</php> to <php>spl_autoload_unregister()</php> is deprecated. Passing <php>spl_autoload_call()</php> to <php>spl_autoload_unregister()</php> is deprecated.
 +
 +The current RFC as proposed has a large BC break for the code:
 +
 +<PHP>
 +
 +namespace foo;
 +
 +var_dump(function_exists('foo\strlen'));
 +var_dump(strlen('x'));
 +var_dump(function_exists('foo\strlen'));
 +
 +if (true) {
 +    function strlen($x) { return 42; }
 +    var_dump(function_exists('foo\strlen'));
 +}
 +
 +var_dump(strlen('x'));
 +
 +</PHP>
 +
 +Which needs to be thought about.
  
 ===== Proposed PHP Version ===== ===== Proposed PHP Version =====
Line 283: Line 304:
 ==== Higher performance through maps ==== ==== Higher performance through maps ====
  
-There is an ongoing conversation, and a [[previous RFC|https://wiki.php.net/rfc/autoload_classmap]] about adding functions to have a native classmap/functionmap/typemap resolver written in C. That is outside the scope of this RFC.+There is an ongoing conversation, and a [[https://wiki.php.net/rfc/autoload_classmap|previous RFC]] about adding functions to have a native classmap/functionmap/typemap resolver written in C. That is outside the scope of this RFC. 
 + 
 +However, a basic implementation of that RFC has been created and can be tried with the implementation of this RFC: https://gitlab.com/Girgias/php-autoloading-maps-extension
  
 ====== Deprecating the SPL autoloader functions ====== ====== Deprecating the SPL autoloader functions ======
Line 394: Line 417:
 For programs that do not have a function autoloader registered, there will be no autoloader to dispatch, so there will be almost no performance change. For programs that do not have a function autoloader registered, there will be no autoloader to dispatch, so there will be almost no performance change.
  
-Whether or not a fuction autoloader is registered, resolving the function is only done once per function per namespace, assuming the function is either loaded or resolved through the global fallback.+Whether or not a function autoloader is registered, resolving the function is only done once per function per namespace, assuming the function is either loaded or resolved through the global fallback.
  
 As the code example shows, after a successful attempt to autoload a function in a namespace, or the global function fallback occurs, the function is 'pinned' to that function, and subsequent use doesn't trigger autoloading. As the code example shows, after a successful attempt to autoload a function in a namespace, or the global function fallback occurs, the function is 'pinned' to that function, and subsequent use doesn't trigger autoloading.
rfc/core-autoloading.1680527020.txt.gz · Last modified: 2023/04/03 13:03 by girgias