I opened a bug (http://bugs.php.net/bug.php?id=33459) to report an issue with
the default behavior of class_exists calling __autoload in PHP5. The bug was
marked as bogus, and basically stated that it's PEARs problem. After responding,
the bug can now longer be searched for.
I believe this still requires some attention as the issues around it are very
similar in nature to the ones with instanceOf and __autoload. (See
http://news.php.net/php.internals/16746)
Basically, adding __autoload to any script using
Calendar/DB_DataObject/SOAP/Validate (just to name a few) will throw a fatal error.
Example being:
<?php
require_once('Calendar/Calendar.php');
$cal = new Calendar();
function __autoload($name) {
require_once($name);
}
?>
This isn't specific to PEAR however... any script/class that uses
!class_exists() to load a file not in 'include_path' will fail when __autoload
is used in conjunction.
Furthermore, I fail to see why such a "feature" is in place when it makes all
existing if(!class_exists()) {} constructs worthless.
if (!class_exists($className)) {
// ... do something interesting ...
// well, with autoload defined, this block will never be reached.
// autoload will either load the class and the above condition will
// be false, or autoload will throw a fatal error and we never get here
}
I just don't understand the logic behind throwing a fatal error when I'm writing
code to try and prevent one from occurring.
I realize a second parameter is available to class_exists that can be set to
false, which will prevent __autoload from being called, but developers don't
always have the luxury of modifying other peoples code to make it work they way
they intended it (such is the case with PEAR classes).
If someone really wants this behavior (though I can't understand for life of me
why), then allow them to pass TRUE
as the second parameter, but leave the
default be false. Or at the very least let autoload return false, so that the
rest of the script can do it's thing... or maybe allow for a php.ini option to
set the default behavior to false (though this option can make some scripts less
portable).
-james
Hi James,
function __autoload($name) {
require_once($name);
}
I just don't understand the logic behind throwing a fatal error when I'm
writing code to try and prevent one from occurring.
You should add error checking to your autoload function. Either by some
file_exists()
/is_readable() check or by using include instead of require
(maybe even with suppressing errors if you dislike is_readable()
...)
For more please ask on the generals list or some other support forum.
johannes
autoload("Class_Name");
defaults to loading Class_Name.php ?
if __autoload exists, it calls that..
adding all sorts of magic to the Class operators is digging a hole that
you will be continually answering...
</rant>
Ok - got up the wrong side of bed this morning ;)
Regards
Alan
I opened a bug (http://bugs.php.net/bug.php?id=33459) to report an issue with
the default behavior of class_exists calling __autoload in PHP5. The bug was
marked as bogus, and basically stated that it's PEARs problem. After responding,
the bug can now longer be searched for.I believe this still requires some attention as the issues around it are very
similar in nature to the ones with instanceOf and __autoload. (See
http://news.php.net/php.internals/16746)Basically, adding __autoload to any script using
Calendar/DB_DataObject/SOAP/Validate (just to name a few) will throw a fatal error.Example being:
<?php
require_once('Calendar/Calendar.php');$cal = new Calendar();
function __autoload($name) {
require_once($name);
}
?>This isn't specific to PEAR however... any script/class that uses
!class_exists() to load a file not in 'include_path' will fail when __autoload
is used in conjunction.Furthermore, I fail to see why such a "feature" is in place when it makes all
existing if(!class_exists()) {} constructs worthless.if (!class_exists($className)) {
// ... do something interesting ...
// well, with autoload defined, this block will never be reached.
// autoload will either load the class and the above condition will
// be false, or autoload will throw a fatal error and we never get here
}I just don't understand the logic behind throwing a fatal error when I'm writing
code to try and prevent one from occurring.I realize a second parameter is available to class_exists that can be set to
false, which will prevent __autoload from being called, but developers don't
always have the luxury of modifying other peoples code to make it work they way
they intended it (such is the case with PEAR classes).If someone really wants this behavior (though I can't understand for life of me
why), then allow them to passTRUE
as the second parameter, but leave the
default be false. Or at the very least let autoload return false, so that the
rest of the script can do it's thing... or maybe allow for a php.ini option to
set the default behavior to false (though this option can make some scripts less
portable).-james