Edit report at https://bugs.php.net/bug.php?id=61265&edit=1
ID: 61265
Comment by: jwalton at m3hc dot com
Reported by: manchokapitancho at gmail dot com
Summary: __autoload should know about traits
Status: Open
Type: Feature/Change Request
Package: Class/Object related
PHP Version: 5.4.0
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2013-06-24 07:22:18] jwalton at m3hc dot com
Actually, the ambiguous ones (i.e. is_a) do not call the autoload.
------------------------------------------------------------------------
[2012-03-26 22:04:42] phristen at yahoo dot com
Ok, my point is still valid though.
Imagine I used is_a($a, "A", true) instead.
------------------------------------------------------------------------
[2012-03-26 03:42:08] phpmpan at mpan dot pl
`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.
------------------------------------------------------------------------
[2012-03-25 17:10:13] phristen at yahoo dot com
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) {
}
------------------------------------------------------------------------
[2012-03-08 14:39:39] manchokapitancho at gmail dot com
Yes, there is a use case.
Currently, I use a workaround but in general, the idea behind is that after I
locate the file, corresponding to the class name being autoloaded,
I require_once the file and then check if the class is really inside
(class_exists). As of 5.4, class_exists returns false for traits and I have to
use
class_exists || trait_exists. Not a big deal, really, but it is still possible
to
have different paths for classes (ex. /classes) and different one for traits
(/traits).
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=61265
--
Edit this bug report at https://bugs.php.net/bug.php?id=61265&edit=1