|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-03-03 13:10 UTC] manchokapitancho at gmail dot com
Description: ------------ Currently, __autoload is automatically called when a class is not found. As of 5.4.0, it is also called when a trait is not found. It is also called when an interface is not found. Unfortunately, there is no way to understand what type of resource is being autoloaded. So I suggest that a second optional argument is added to __autoload. It can receive three possible values from the PHP engine - for example: AUTOLOAD_CLASS, AUTOLOAD_TRAIT and AUTOLOAD_INSTANCE. This way, a better autoload handling can be achieved. Patchesautoload.patch (last revision 2013-06-24 07:22 UTC by jwalton at m3hc dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 10:00:01 2025 UTC |
This would certainly be a nice feature. However, keep in mind that it is not always possible to tell whether something is a class or an interface. For example, when doing something like this, we can't tell whether 'A' is an interface or a class: if ($a instanceof A) { }`instanceof` operator does not trigget class loading: ----------------------------------------- class X {} function __autoload($name) { echo "Loading $name...\n"; } $x = new X(); if ($x instanceof X) { echo 'x is X'; } if ($x instanceof NonExistingClass) { echo 'blarg'; } ----------------------------------------- The same applies to argument types.Added a patch, where autoloading will pass along the type its looking for. Should be fixed to have constants, and a fix of err_012.phpt test should be fixed. Quick and dirty though, and I'll clean it up if someone wants it. function __AUTOLOAD($typename, $type) { } or you can use spl as well spl_autoload_register(); $type = 0, Class $type = 1, Interface $type = 2, Trait class Test extends Test2 {}; $type = 0 for loading Test2 class Test implements Test2 {}; $type = 1 for loading Test2 class Test { use Test2; }; $type = 2 for loading Test2 class_exists("A"); $type = 0 for loading A interface_exists("A"); $type = 1 for loading A trait_exists("A"); $type = 2 for loading A This patch is for php-5.5.0 release.$extension = array(".pclass", ".pinterface", ".ptrait"); spl_autoload_register(function($classname, $type) { global $includepath; if( $type >=0 && $type <= 2 ) { $filename = "$includepath/".strtolower($classname).$extension[$type]; if( is_readable($filename) ) { include_once($filename); } } }); You could also use spl_autoload_extensions(".pclass,.pinterface,.ptrait") or spl_autoload_register(function($classname, $type) { global $includepath; $filename = "$includepath/".strtolower($classname); if( is_readable("$filename.pclass" ) { include_once("$filename.pclass"); } else if( is_readable("$filename.pinterface") ) { include_once("$filename.pinterface"); } else if( is_readable("$filename.ptrait") ) { include_once("$filename.ptrait"); } }); This will make zero difference if all your files are classes (type doesn't mean anything, but its about 20-30% more efficient when you start including interfaces and traits, because there are now less stat calls being done on the filesystem. This code allows for only one stat needed per call, instead of 1-3.