rfc:function_autoloading4
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:function_autoloading4 [2024/08/15 15:10] – withinboredom | rfc:function_autoloading4 [2024/09/04 19:07] (current) – fix example withinboredom | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Version: 1.0 | * Version: 1.0 | ||
* Date: 2024-08-15 | * Date: 2024-08-15 | ||
- | * Author: Robert Landers, < | + | * Author: Robert Landers, < |
* Status: Under Discussion (or Accepted or Declined) | * Status: Under Discussion (or Accepted or Declined) | ||
* First Published at: http:// | * First Published at: http:// | ||
Line 10: | Line 10: | ||
The topic of supporting function autoloading was brought up many times in the past, this RFC introduces a potential implementation which would be consistent with what we have for autoloading classes. | The topic of supporting function autoloading was brought up many times in the past, this RFC introduces a potential implementation which would be consistent with what we have for autoloading classes. | ||
+ | |||
+ | By using autoloaders, | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | The suggested change would be pretty straightforward and backwards-compatible: | + | This RFC proposes to add two new constants to the SPL extension: '' |
- | + | ||
- | - Add two new constants to spl: SPL_AUTOLOAD_CLASS, | + | |
- | - Add a fourth optional parameter for spl_autoload_register, | + | |
- | - The type for the missing token should also be passed to the $autoload_function callback as a second param. (e.g., SPL_AUTOLOAD_CLASS | + | |
- | - Change the current class autoloading to only call the autoloaders which match with the SPL_AUTOLOAD_CLASS | + | |
- | - Add the function autoloading to only call the autoloaders which match with the SPL_AUTOLOAD_FUNCTION types. | + | |
- | + | ||
- | There won’t | + | |
- | - is called in a fully qualified form (e.g., a '' | + | There won’t be any changes to the current autoloading mechanism when it comes to classes. |
- | - is not defined | + | |
- | - and an autoloader is registered with the SPL_AUTOLOAD_FUNCTION type | + | |
- | then the autoloader will be called with the function name as the first parameter (with the initial slash removed) and SPL_AUTOLOAD_FUNCTION as the second parameter. | + | ==== Function Autoloading ==== |
- | However, if a function | + | The function |
- | - is called in an unqualified | + | PHP allows programmers to call an unqualified |
- | - is not defined locally or globally | + | |
- | - and an autoloader is registered with the SPL_AUTOLOAD_FUNCTION type | + | |
- | + | ||
- | then the autoloader will be called | + | |
Example "'' | Example "'' | ||
Line 42: | Line 30: | ||
<?php | <?php | ||
- | spl_autoload_register(function ($function, $type) { | + | spl_autoload_register(function ($function) { |
- | | + | $function_path = dirname(str_replace(' |
- | | + | $file = __DIR__ . '/ |
- | $file = __DIR__ . '/ | + | |
- | | + | |
- | require_once $file; | + | require_once $file; |
- | } | + | |
} | } | ||
}, false, false, SPL_AUTOLOAD_FUNCTION); | }, false, false, SPL_AUTOLOAD_FUNCTION); | ||
</ | </ | ||
+ | |||
+ | ==== Performance Impact ==== | ||
+ | |||
+ | Function autoloading doesn’t appear to have a significant impact on performance; | ||
+ | |||
+ | To help mitigate any potential performance impact of function autoloading many unqualified functions, a function will only be searched for once per namespace. | ||
+ | |||
+ | ==== spl_autoload ==== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | ==== spl_autoload_unregister ==== | ||
+ | |||
+ | '' | ||
+ | |||
+ | ==== spl_autoload_functions ==== | ||
+ | |||
+ | '' | ||
+ | |||
+ | ==== spl_autoload_call ==== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | In the event that both constants are passed, it will attempt to autoload both types. | ||
+ | |||
+ | <code php> | ||
+ | spl_autoload_call(' | ||
+ | spl_autoload_call(' | ||
+ | spl_autoload_call(' | ||
+ | spl_autoload_call(' | ||
+ | </ | ||
+ | |||
+ | ==== function_exists ==== | ||
+ | |||
+ | The '' | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | There are no backward incompatible changes. | + | There shouldn’t be any backward incompatible changes. |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 66: | Line 87: | ||
==== To Opcache ==== | ==== To Opcache ==== | ||
- | To be determined. | + | * Potential changes to JIT helpers to call the autoloader instead of reading from the function table directly. |
==== New Constants ==== | ==== New Constants ==== | ||
Line 74: | Line 95: | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | To be determined. | + | None. |
===== Future Scope ===== | ===== Future Scope ===== | ||
Line 81: | Line 102: | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
+ | |||
+ | As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted. | ||
+ | |||
+ | Voting started on 2023-XX-XX and will end on 2023-XX-XX. | ||
+ | |||
+ | |||
+ | |||
<doodle title=" | <doodle title=" | ||
Line 86: | Line 114: | ||
* No | * No | ||
</ | </ | ||
+ | |||
+ | |||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | Not yet. | + | Review the implementation [[https:// |
===== Implementation ===== | ===== Implementation ===== | ||
- | After the project is implemented, | + | * Implentation: |
+ | * Version: TBD | ||
+ | * PHP Manual Entry: TODO | ||
===== References ===== | ===== References ===== | ||
Line 105: | Line 137: | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
- | Keep this updated with features that were discussed on the mail lists. | + | ==== Autoloading constants ==== |
+ | |||
+ | Autoloading of other types such as constants and stream wrappers will come in a later RFC. | ||
rfc/function_autoloading4.1723734658.txt.gz · Last modified: 2024/08/15 15:10 by withinboredom