RE: [PHP-DEV] autoloading and undefined class constants
| From: | Ben Bidner | Date: | Mon, 06 Jul 2009 04:21:27 +0000 |
| Subject: | RE: [PHP-DEV] autoloading and undefined class constants | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-44716@lists.php.net to get a copy of this message | ||
> per the manual, exceptions thrown in an autoload method are swallowed,
> and an E_ERROR is triggered by php.
>
> http://us2.php.net/manual/en/language.oop5.autoload.php
I have read that note before, and wondered exactly what it was referring to since you can throw
exceptions within an autoloader and catch them (or have your exception handler do whatever it needs
to do with them).
<?php
function autoloader($className)
{
echo "autoloading" . PHP_EOL;
throw new Exception("Fail");
}
spl_autoload_register("autoloader");
try
{
// Exception
$obj = new NonExistentClass;
}
catch(Exception $e)
{
echo "caught" . PHP_EOL;
}
try
{
// Exception
$const = constant("NonExistentClass::NON_EXISTENT_CONSTANT");
}
catch(Exception $e)
{
echo "caught" . PHP_EOL;
}
try
{
// Fatal error
$const = NonExistentClass::NON_EXISTENT_CONSTANT;
}
catch(Exception $e)
{
echo "never happens" . PHP_EOL;
}
?>
Will output:
autoloading
caught
autoloading
caught
autoloading
PHP Fatal error: Undefined class constant
I would expect accessing a constant directly to behave as constant() does at the moment .... and I
would expect constant() to behave as the manual says it does (and return NULL for non existent class
constants instead of throwing E_ERRORs).
Ben Bidner
+61.7.3161.2000 (office) | +61.7.3009.0651 (fax) | +61.4.3201.5362 (mobile)
http://www.vuetec.com/