Is there a way I can define __autoload
in a class, so whenever I access an undefined class PHP will run the __autoload
class method?
<?php
class Test {
public function __construct() {
echo 'Instantiating ', __CLASS__, "
";
}
public function __autoload($className) {
echo "Loading $className
";
require_once($className . '.php');
}
public function test() {
$test = new AnotherClass();
}
}
$bill = new Test();
$bill->test();
AnotherClass.php:
<?php
class AnotherClass {
public function __construct() {
echo 'Instantiating ', __CLASS__, "
";
}
}
Output:
Instantiating Test Loading AnotherClass Instantiating AnotherClass