rfc:function_autoloading
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:function_autoloading [2013/09/03 12:28] – Withdrawn ircmaxell | rfc:function_autoloading [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Function Autoloading ====== | ====== PHP RFC: Function Autoloading ====== | ||
- | * Version: 0.2 | + | * Version: 0.3 |
- | * Date: 2013-08-29 | + | * Date: 2015-09-04 |
* Author: Anthony Ferrara < | * Author: Anthony Ferrara < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 11: | Line 11: | ||
PHP presently offers the ability to import class-like structures (classes, interfaces and traits) via a callback (or series of them) that can be registered. This lets a developer " | PHP presently offers the ability to import class-like structures (classes, interfaces and traits) via a callback (or series of them) that can be registered. This lets a developer " | ||
- | Presently, other types of symbols tables are not autoloadable. This RFC proposes a new unified autoloading mechanism to unify autoloading efforts across all three symbol tables (class, function and constant). | + | Presently, other types of symbols tables are not autoloadable. This RFC proposes a new unified autoloading mechanism to unify autoloading efforts across all four symbol tables (class, function, stream wrapper |
===== Proposal ===== | ===== Proposal ===== | ||
Line 21: | Line 21: | ||
This proposal registers the following constants: | This proposal registers the following constants: | ||
- | * //php\AUTOLOAD_ALL | + | |
- | | + | * //php\AUTOLOAD_FUNCTION |
- | | + | * //php\AUTOLOAD_CONSTANT |
- | | + | * //php\AUTOLOAD_STREAM |
- | ==== Functions ==== | + | ==== Userland |
- | This proposal registers the following functions: | + | This proposal registers |
- | === bool php\autoload_register(callable $callback, int $type = php\AUTOLOAD_ALL, bool $prepend = false) === | + | === bool php\autoload_register(callable $callback, int $type, bool $prepend = false) === |
- | This function behaves similar to the current // | + | This function behaves similar to the current // |
- | === bool php\autoload_unregister(callable $callback) === | + | === bool php\autoload_unregister(callable $callback, int $type = 0) === |
- | This function behaves similar to the current // | + | This function behaves similar to the current // |
- | ==== Behavior ==== | + | === array php\autoload_list(int $type) |
- | Registering autoloaders with the new API will allow callbacks to be fired on class, | + | This function |
- | === Default Behavior | + | === function_exists() |
- | The default behavior | + | A new optional boolean argument |
- | <file php basic_usage.php> | + | === defined() === |
- | <?php | + | |
- | php\autoload_register(function($name, $type) { | + | A new optional boolean argument is added to //defined()// to match the behavior of // |
- | | + | |
- | | + | ==== Internal Typedefs ==== |
- | case php\AUTOLOAD_FUNCTION: | + | |
- | | + | This proposal adds the following internal structures: |
- | | + | |
- | case php\AUTOLOAD_CLASS: | + | === zend_autoload_func === |
- | eval("class $name {}"); | + | |
- | | + | This structure stores the autoloader function. |
- | case php\AUTOLOAD_CONSTANT: | + | |
- | | + | <code c> |
- | | + | typedef struct zend_autoload_func |
- | } | + | |
- | }); | + | zend_fcall_info_cache fcc; |
- | foo(); // string(3) " | + | zval *callable; |
- | new foo(); // string(3) " | + | int pass_type; |
- | foo; // string(3) " | + | } _zend_autoload_func; |
- | ?> | + | </ |
- | </file> | + | |
+ | ==== Internal Functions ==== | ||
+ | |||
+ | This proposal adds the following ZEND_API functions/ | ||
+ | |||
+ | === void* zend_autoload_call(zend_string *name, zend_string *lname, int type) === | ||
+ | |||
+ | This function will call an autoloader of the specified type. It requires two versions of the name, one which is the called case, and one which is the normalized lookup case. In the case of case-sensitive constants, they should be identical. In the case of insensitive constants, functions and classes, //lname// should be a lowercase version of the name. | ||
+ | |||
+ | === int zend_autoload_register(zend_autoload_func* func, int type, int flags) === | ||
+ | |||
+ | This will register an autoload | ||
+ | |||
+ | === int zend_autoload_unregister(zend_autoload_func* func, int type) === | ||
+ | |||
+ | This will unregister an autoloader function that's passed. | ||
+ | |||
+ | === int zend_lookup_function(const char *name, int name_length, | ||
+ | |||
+ | Lookup a function by name, using autoloading | ||
+ | |||
+ | === int zend_lookup_function_ex(const char *name, int name_length, | ||
+ | |||
+ | Lookup a function by name, optionally calling the autoloader. | ||
+ | |||
+ | === ZEND_LOOKUP_FUNCTION_BY_NAME(name, fbc) === | ||
+ | |||
+ | This macro will find a function in the symbol table, or attempt to autoload it if not defined. The name must be a zend_string. | ||
+ | |||
+ | === ZEND_LOOKUP_FUNCTION_BY_KEY(name, key, fbc) === | ||
+ | |||
+ | This macro will find a function in the symbol table, or attempt to autoload it if not defined. This separates the called function in //name// from the looked up function in //key//. Both name and key must be zend_string. | ||
+ | |||
+ | === ZEND_LOOKUP_FUNCTION_BY_NS_KEY(name, key, fbc) === | ||
+ | |||
+ | This macro will find a function in the symbol table, or attempt to autoload it if not defined. This separates the called function in //name// from the looked up function in //key//. Both name and key must be zend_string. | ||
+ | |||
+ | ==== Behavior ==== | ||
+ | |||
+ | Registering autoloaders with the new API will allow callbacks to be fired on class, function and/or constant missing errors. | ||
=== Single Type Behavior === | === Single Type Behavior === | ||
Line 80: | Line 119: | ||
}, php\AUTOLOAD_FUNCTION); | }, php\AUTOLOAD_FUNCTION); | ||
foo(); // string(3) " | foo(); // string(3) " | ||
- | new foo(); // FATAL_ERROR | + | new foo(); // FATAL_ERROR |
?> | ?> | ||
</ | </ | ||
Line 86: | Line 125: | ||
=== Multiple Type Behavior === | === Multiple Type Behavior === | ||
- | By passing a bitwise | + | By passing a bitwise-or'd constant |
<file php multiple_type.php> | <file php multiple_type.php> | ||
Line 93: | Line 132: | ||
var_dump($name, | var_dump($name, | ||
switch ($type) { | switch ($type) { | ||
- | | + | case php\AUTOLOAD_FUNCTION: |
- | eval(" | + | |
- | break; | + | |
- | case php\AUTOLOAD_CONSTANT: | + | |
- | define($name, | + | |
- | break; | + | |
} | } | ||
}, php\AUTOLOAD_FUNCTION | php\AUTOLOAD_CONSTANT); | }, php\AUTOLOAD_FUNCTION | php\AUTOLOAD_CONSTANT); | ||
foo(); // string(3) " | foo(); // string(3) " | ||
- | foo; // string(3) "foo" int(3) | + | FOO; // string(3) "FOO" int(4) |
- | new foo(); // FATAL_ERROR | + | new foo(); // FATAL_ERROR |
?> | ?> | ||
</ | </ | ||
- | === Registering The Same Callback Multiple Times === | + | === Registering The Same Callback Multiple Times For Different Types === |
- | Only the first registration of a callback will succeed: | + | <file php multiple_registration.php> |
- | + | ||
- | <file php single_registration.php> | + | |
<?php | <?php | ||
$callback = function($name, | $callback = function($name, | ||
- | var_dump($name, $type); | + | var_dump($name); |
- | | + | |
- | | + | eval(" |
- | | + | } else { |
- | | + | define($name, |
- | case php\AUTOLOAD_CLASS: | + | |
- | eval(" | + | |
- | break; | + | |
- | | + | |
- | | + | |
- | break; | + | |
} | } | ||
}; | }; | ||
php\autoload_register($callback, | php\autoload_register($callback, | ||
- | php\autoload_register($callback, | + | php\autoload_register($callback, |
foo(); // string(3) " | foo(); // string(3) " | ||
- | foo; // FATAL_ERROR | + | FOO; // string(3) " |
?> | ?> | ||
</ | </ | ||
- | |||
- | ==== Performance ==== | ||
- | |||
- | The following benchmarks are all run on a non-debug build compiled with // | ||
- | |||
- | === Class Loading === | ||
- | |||
- | 1000 classes were generated, each in a single file. The following test script was used to execute the following tests: | ||
- | |||
- | <file php benchmark_autoloading.php> | ||
- | <?php | ||
- | spl_autoload_register(function($class) { | ||
- | require __DIR__ . '/ | ||
- | }); | ||
- | |||
- | $start = microtime(true); | ||
- | for ($i = 0; $i < 1000; $i++) { | ||
- | $class = ' | ||
- | new $class; | ||
- | } | ||
- | $end = microtime(true); | ||
- | |||
- | echo " | ||
- | ?> | ||
- | </ | ||
- | |||
- | * Master' | ||
- | * Proposed // | ||
- | |||
- | Therefore, there is no performance regression when autoloading classes (in fact, it is slightly improved, since one additional // | ||
- | |||
- | === Functions === | ||
- | |||
- | 1000 functions were generated and placed in a single file. The following test script was used to test if there was any change to function call time for a defined function: | ||
- | |||
- | <file php benchmark_functions.php> | ||
- | <?php | ||
- | require_once ' | ||
- | |||
- | $start = microtime(true); | ||
- | for ($i = 0; $i < 1000; $i++) { | ||
- | $func = ' | ||
- | $func(); | ||
- | } | ||
- | $end = microtime(true); | ||
- | |||
- | echo " | ||
- | ?> | ||
- | </ | ||
- | |||
- | * Master: average 0.000216 Seconds to call 1000 functions | ||
- | * Proposal: average 0.000218 Seconds to call 1000 functions | ||
- | |||
- | |||
- | Therefore, there is no performance regression to normal function calls (defined functions). The margin of error for this test was on the order of +-0.00004 seconds. | ||
- | |||
- | === Constants === | ||
- | |||
- | 1000 constants were generated and placed in a single file. The following test script was used to test if there was any change to function call time for a defined function: | ||
- | |||
- | <file php benchmark_constants.php> | ||
- | <?php | ||
- | require_once ' | ||
- | |||
- | $start = microtime(true); | ||
- | for ($i = 0; $i < 1000; $i++) { | ||
- | $name = ' | ||
- | constant($name); | ||
- | } | ||
- | $end = microtime(true); | ||
- | |||
- | echo " | ||
- | ?> | ||
- | </ | ||
- | |||
- | * Master: average 0.000228 Seconds to call 1000 constants | ||
- | * Proposal: average 0.000221 Seconds to call 1000 constants | ||
- | |||
- | Therefore, there is no performance regression to normal constant lookups (defined constants). | ||
==== Userland Backwards Compatibility ==== | ==== Userland Backwards Compatibility ==== | ||
Line 219: | Line 171: | ||
This RFC proposes to strip the current // | This RFC proposes to strip the current // | ||
- | This means that calls to // | + | This means that calls to // |
=== __autoload() === | === __autoload() === | ||
Line 258: | Line 210: | ||
This implementation greatly simplifies the (internal) handling of autoloading in general. | This implementation greatly simplifies the (internal) handling of autoloading in general. | ||
- | ==== Why support multiple " | + | ==== Why not support multiple " |
- | It is more of a "why not" question. Supporting multiple types of autoloaded constructs in a single callback can result in a more flexible architecture for end users. | + | Existing autoloaders may support |
==== What Filename Conventions Does This Support? ==== | ==== What Filename Conventions Does This Support? ==== | ||
Line 306: | Line 258: | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | PHP 5.6.x | + | PHP 7.1.x |
===== SAPIs Impacted ===== | ===== SAPIs Impacted ===== | ||
Line 315: | Line 267: | ||
See Backward Incompatible Changes | See Backward Incompatible Changes | ||
- | |||
- | ===== New Constants ===== | ||
- | |||
- | * // | ||
- | * // | ||
- | * // | ||
- | * // | ||
===== php.ini Defaults ===== | ===== php.ini Defaults ===== | ||
Line 365: | Line 310: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | There is a proof-of-concept | + | A patch will be created shortly |
- | + | ||
- | This patch is not production ready, but serves as a demonstration of the functionality. | + | |
===== References ===== | ===== References ===== | ||
Line 385: | Line 328: | ||
* 2013-08-29 0.1 Initial Creation | * 2013-08-29 0.1 Initial Creation | ||
* 2013-08-30 0.2 Add performance section and basic benchmarks | * 2013-08-30 0.2 Add performance section and basic benchmarks | ||
+ | * 2015-09-04 0.3 Re-proposed |
rfc/function_autoloading.1378211306.txt.gz · Last modified: 2017/09/22 13:28 (external edit)