Hi again,
The attached patches for PHP 5.3 and PHP 6 add in_class_exists(). The
function is intended to be used in __autoload(). The following test
script demonstrates its usage:
<?php
var_dump(in_class_exists('test'));
function __autoload($class)
{
var_dump(in_class_exists($class));
eval ('class ' . $class . ' {}');
}
$a = new Bungalow; // autoload called, not because of class_exists()
,
bool(false)
class_exists('test'); // autoload called because of class_exists()
,
bool(true)
class_exists('test'); // class exists, no autoload, no output
$a = new test; // autoload not called, no output
$a = new Another; // autoload called, not because of class_exists()
,
bool(false)
?>
outputting:
bool(false)
bool(false)
bool(true)
bool(false)
The purpose is to allow an autoload handler to detect whether it is safe
to use die() or some other method of terminating execution. A user
calling class_exists()
clearly expects a return from autoload, whereas a
user instantiating an object will expect a fatal error if the class
cannot be found.
This is useful for PEAR2 as a means of displaying helpful information on
locating missing class files only in cases where a fatal error would
result anyways.
Thanks,
Greg
to use die() or some other method of terminating execution. A user
callingclass_exists()
clearly expects a return from autoload, whereas a
user instantiating an object will expect a fatal error if the class
cannot be found.
This is true, but __autoload should never do it, the engine should do
it. So I think it's better just not make autoloader ever produce
errors. Can't find it - just give up, if engine really needs it it'd
bail out :)
This is useful for PEAR2 as a means of displaying helpful information on
locating missing class files only in cases where a fatal error would
result anyways.
Well, autoloader might print debug traces etc. if the developer wants
it, but I don't see why this should be unique feature for class_exists.
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
Stanislav Malyshev wrote:
to use die() or some other method of terminating execution. A user
callingclass_exists()
clearly expects a return from autoload, whereas a
user instantiating an object will expect a fatal error if the class
cannot be found.This is true, but __autoload should never do it, the engine should do
it. So I think it's better just not make autoloader ever produce
errors. Can't find it - just give up, if engine really needs it it'd
bail out :)
Another possibility: could there be a way to add context information (a
string) to the error message the engine displays? This would alleviate
my concern.This is useful for PEAR2 as a means of displaying helpful information on
locating missing class files only in cases where a fatal error would
result anyways.Well, autoloader might print debug traces etc. if the developer wants
it, but I don't see why this should be unique feature for class_exists.
the point is that if the user is using class_exists()
they don't want
extra output on missing class.
Greg
Another possibility: could there be a way to add context information (a
string) to the error message the engine displays? This would alleviate
my concern.
Why not just log it into some logfile? Remember that autoloaders can be
chained, so engine would have to assemble all strings, display them in
order, etc. Why create another mini-logging system?
the point is that if the user is using
class_exists()
they don't want
extra output on missing class.
But how would you know the class is missing? Maybe next loader in chain
would find it. Anyway, this looks like debug feature, why shouldn't it
be handled on application level?
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
Hi,
What about __class_exists() and SPL_register_class_exists_handler()?
One could suppose the classloader (the one that implements
__autoload) knows if a class exists (as in: is registered within the
classloader or the class-file exists) without loading it and maybe
the class that calls class_exists()
never uses it.
regards,
Benjamin