rfc:function_autoloading

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
rfc:function_autoloading [2015/09/03 04:51] – Add streams ircmaxellrfc:function_autoloading [2017/09/22 13:28] (current) – external edit 127.0.0.1
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 "catch" the fact that a class isn't present, and gives them a chance to load it. This is used to great effect in the PHP community. 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 "catch" the fact that a class isn't present, and gives them a chance to load it. This is used to great effect in the PHP community.
  
-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, stream wrapper 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 and constant).
  
 ===== Proposal ===== ===== Proposal =====
Line 23: Line 23:
   * //php\AUTOLOAD_CLASS => 1// - Represents Class autoloading   * //php\AUTOLOAD_CLASS => 1// - Represents Class autoloading
   * //php\AUTOLOAD_FUNCTION => 2// - Represents Function autoloading   * //php\AUTOLOAD_FUNCTION => 2// - Represents Function autoloading
-  * //php\AUTOLOAD_CONSTANT => 3// - Represents Constant autoloading +  * //php\AUTOLOAD_CONSTANT => 4// - Represents Constant autoloading 
-  * //php\AUTOLOAD_STREAM = 4// - Represents Stream autoloading+  * //php\AUTOLOAD_STREAM = 8// - Represents Stream autoloading
  
 ==== Userland Functions ==== ==== Userland Functions ====
Line 32: Line 32:
 === bool php\autoload_register(callable $callback, int $type, bool $prepend = false) === === bool php\autoload_register(callable $callback, int $type, bool $prepend = false) ===
  
-This function behaves similar to the current //spl_autoload_register// function. You *must* call this function individually for the specific autoloader that you want to hook into.+This function behaves similar to the current //spl_autoload_register// function. You can pass in a bitmask of types to register for this autoloader.
  
 === bool php\autoload_unregister(callable $callback, int $type = 0) === === bool php\autoload_unregister(callable $callback, int $type = 0) ===
Line 63: Line 63:
     zend_fcall_info_cache fcc;     zend_fcall_info_cache fcc;
     zval *callable;     zval *callable;
 +    int pass_type;
 } _zend_autoload_func; } _zend_autoload_func;
 </code> </code>
Line 118: Line 119:
 }, php\AUTOLOAD_FUNCTION); }, php\AUTOLOAD_FUNCTION);
 foo(); // string(3) "foo" int(2) foo(); // string(3) "foo" int(2)
 +new foo(); // FATAL_ERROR as no autoloader is registered
 +?>
 +</file>
 +
 +=== Multiple Type Behavior ===
 +
 +By passing a bitwise-or'd constant to the register function, the callback will only be called for types that match).
 +
 +<file php multiple_type.php>
 +<?php
 +php\autoload_register(function($name, $type) {
 +    var_dump($name, $type);
 +    switch ($type) {
 +       case php\AUTOLOAD_FUNCTION:
 +           eval("function $name(){}");
 +           break;
 +       case php\AUTOLOAD_CONSTANT:
 +           define($name, $name);
 +           break;
 +    }
 +}, php\AUTOLOAD_FUNCTION | php\AUTOLOAD_CONSTANT);
 +foo(); // string(3) "foo" int(2)
 +FOO; // string(3) "FOO" int(4)
 new foo(); // FATAL_ERROR as no autoloader is registered new foo(); // FATAL_ERROR as no autoloader is registered
 ?> ?>
Line 126: Line 150:
 <file php multiple_registration.php> <file php multiple_registration.php>
 <?php <?php
-$callback = function($name) {+$callback = function($name, $type) {
     var_dump($name);     var_dump($name);
     if ($name === 'foo') {     if ($name === 'foo') {
Line 147: Line 171:
 This RFC proposes to strip the current //spl_autoload_register// functionality, and make //spl_autoload_*// simple proxies for registering core autoloaders. They will function exactly as they do now, but under the hood they will be using the new interface. This RFC proposes to strip the current //spl_autoload_register// functionality, and make //spl_autoload_*// simple proxies for registering core autoloaders. They will function exactly as they do now, but under the hood they will be using the new interface.
  
-This means that calls to //spl_autoload_functions()// will include any autoloader (which indicates support for //php\AUTOLOAD_CLASS//) registered through //php\autoload_register()//.+This means that calls to //spl_autoload_functions()// will include any autoloader (which indicates support for //php\AUTOLOAD_CLASS//) registered through //php\autoload_register()//. However, all autoloaders registered via //spl_autoload_register// will set the //pass_type// flag to //0//, meaning that only a single argument will be passed to the callback. This is for compatiblity.
  
 === __autoload() === === __autoload() ===
rfc/function_autoloading.1441255899.txt.gz · Last modified: 2017/09/22 13:28 (external edit)