__autoload is called when using class_exists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

__autoload is called when using class_exists

Post 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 ?
Last edited by quocbao on Sun Jun 18, 2006 6:55 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 autocall, its autoload
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

Sorry about autoload :D

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 ?
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Should you be looking at doing

Code: Select all

class_exists('blah', false)
?

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.
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

I already found the solution using trace , but thanks u anyway .
Post Reply