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 21:30] – clarity withinboredom | rfc:function_autoloading4 [2024/09/04 19:07] (current) – fix example withinboredom | ||
---|---|---|---|
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 ===== | ||
- | Before getting into the details, there are a few terms worth acknowledging so that the proposal can be easily discussed without getting confused: | + | This RFC proposes to add two new constants to the SPL extension: '' |
- | + | ||
- | - **Defined function**: A function that the engine has knowledge of, such as in a previously included/ | + | |
- | - **Undefined function**: A function that the engine does not have knowledge of. | + | |
- | - **Function autoloading**: | + | |
- | - **Written function**: A function that exists in a file that the engine may or may not have knowledge of. | + | |
- | - **Local scope**: The current namespace | + | |
- | - **Global scope**: The global namespace ('' | + | |
- | + | ||
- | The suggested change would be pretty straightforward and backwards-compatible: | + | |
- | + | ||
- | - Add two new constants to spl: SPL_AUTOLOAD_CLASS, SPL_AUTOLOAD_FUNCTION. | + | |
- | - 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 | + | |
- | There won’t be any changes to the current autoloading mechanism when it comes to classes. | + | There won’t be any changes to the current autoloading mechanism when it comes to classes. |
- | - is called in a fully qualified form (e.g., a '' | + | ==== Function Autoloading ==== |
- | - 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. | + | The function |
- | However, if a function | + | PHP allows programmers to call an unqualified |
- | + | ||
- | - is called in an unqualified | + | |
- | - is not defined locally | + | |
- | - and an autoloader is registered with the SPL_AUTOLOAD_FUNCTION type | + | |
- | + | ||
- | then the autoloader will be called with the current namespace | + | |
- | + | ||
- | This provides an opportunity for an autoloader to check for the existence of a function in the local scope and define it, as well as defer to the global | + | |
Example "'' | Example "'' | ||
Line 53: | 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-wise, this should have minimal impact on existing codebases as there is no default function autoloader. | + | ==== Performance |
- | For codebases that want to take advantage | + | Function autoloading doesn’t appear |
+ | |||
+ | To help mitigate any potential performance impact | ||
==== spl_autoload ==== | ==== spl_autoload ==== | ||
- | The '' | + | The '' |
+ | |||
+ | ==== spl_autoload_unregister ==== | ||
+ | |||
+ | '' | ||
+ | |||
+ | ==== spl_autoload_functions ==== | ||
+ | |||
+ | '' | ||
==== spl_autoload_call ==== | ==== spl_autoload_call ==== | ||
- | The '' | + | The '' |
+ | |||
+ | In the event that both constants are passed, it will attempt to autoload both types. | ||
<code php> | <code php> | ||
- | spl_autoload_call(' | + | spl_autoload_call(' |
- | spl_autoload_call(' | + | |
- | spl_autoload_call(' | + | |
spl_autoload_call(' | spl_autoload_call(' | ||
- | spl_autoload_call(' | + | spl_autoload_call(' |
+ | spl_autoload_call(' | ||
</ | </ | ||
- | If the user wants to call multiple autoloaders, they can do so manually. | + | ==== 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 99: | 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 107: | Line 95: | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | To be determined. | + | None. |
===== Future Scope ===== | ===== Future Scope ===== | ||
Line 114: | 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=" | ||
* Yes | * Yes | ||
* 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 136: | 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.1723757442.txt.gz · Last modified: 2024/08/15 21:30 by withinboredom