Re: Auto-load class if it doesn't exists!

From: Date: Tue, 16 Nov 2004 22:31:45 +0000
Subject: Re: Auto-load class if it doesn't exists!
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-202234@lists.php.net to get a copy of this message
is it possible to have a solution that works like an autoloader... for example: $myclass = new class(); but if this class wasn't loaded yet, it loads by itself... egg: if(class_exists(class)) {
     $myclass = new class();
} else {
     require_once(PATH_DIR.'class.class.php');
    $myclass = new class();
}
PHP5 has a callback for just this purpose. A special function named __autoload() is called whenever an attempt is made to instantiate an undeclared class. The function should then define the needed class. I've been using it for months without any issue. I just did a search and was suprised to see it's missing from the docs at php.net. It's mentioned on zend.com though: http://www.zend.com/php5/articles/engine2-php5-changes.php#Heading19 --Rick

« previous php.general (#202234) next »