Page 1 of 1
__autoload is called when using class_exists
Posted: Sun Jun 18, 2006 6:37 pm
by quocbao
Anyone know that __autoload is called when you use class_exists function
Code: Select all
function __autoload ($name)
{
require_once($name);
}
if (class_exists($name))
{
//do something
}
I just want to check whether this class is existed or not but i end with a PHP E_ERROR

, can someone help me ?
Posted: Sun Jun 18, 2006 6:44 pm
by John Cartwright
You got the logic a bit backwards here,
Code: Select all
function __autoload($name)
{
require_once($name.'.php');
if (!class_exists($name))
{
trigger_error('Class '.$name.' not found in '.$name.'.php', E_USER_FATAL);
}
}
$foo = new foo();
By the way, it's not auto
call, its auto
load
Posted: Sun Jun 18, 2006 6:54 pm
by quocbao
Sorry about autoload
Code: Select all
function __autoload($name)
{
require_once($name.'.php');
if (!class_exists($name))
{
trigger_error('Class '.$name.' not found in '.$name.'.php', E_USER_FATAL);
}
}
//$foo = new foo();
if (class_exists('foo'))
{
//fatal
}
Sometime we want to check a class existance , but end up with USER_FATAL

. I surprise that __autoload is also called when we use class_exists . So we can't use class_exists when we have an autoload function

, huh ?
Posted: Sun Jun 18, 2006 7:49 pm
by sweatje
Should you be looking at doing
?
http://php.net/class_exists
manual wrote:class_exists() will attempt to call __autoload by default, if you don't want class_exists() to call __autoload, you can set the parameter autoload to FALSE.
Posted: Mon Jun 19, 2006 1:11 pm
by quocbao
I already found the solution using trace , but thanks u anyway .