note 93057 deleted from language.oop5.autoload by cmb
| From: | cmb@php.net | Date: | Sun, 26 May 2019 09:09:23 +0000 |
| Subject: | note 93057 deleted from language.oop5.autoload by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211128@lists.php.net to get a copy of this message | ||
Note Submitter: h762017(at)stud(dot)u-szeged(dot)hu
----
Hi,
Because the scripting engine just find the class declaration in the body of the __autoload()
function, you also can declare the missing class in the __autoload() function. (No need to include
or require a file.)
Let's see the following code:
Example 1.:
<?php
function __autoload($className){
echo "Now loading: $className<br />";
class SuperClass {
}
}
class DerivedClass extends SuperClass {
}
class AnotherDerivedClass extends SuperClass {
}
?>
The scripting engine will found the SuperClass class.
Example 2.:
You also can do it with the eval function, and if you dinamycally declare the class, you don't
get a Fatal Error, and you can do many interesting things with the eval function;)
<?php
function __autoload($className){
echo "Now loading: $className<br />";
eval("class $className {}");
}
class DerivedClass extends SuperClass {
}
class AnotherDerivedClass extends SuperClass {
}
?>