note 93057 added to language.oop5.autoload

From: Date: Fri, 21 Aug 2009 02:24:11 +0000
Subject: note 93057 added to language.oop5.autoload
Groups: php.notes 
Request: Send a blank email to php-notes+get-159724@lists.php.net to get a copy of this message
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 { } ?> ---- Server IP: 209.41.74.194 Probable Submitter: 62.68.186.60 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php Edit -- https://master.php.net/note/edit/93057 Del: integrated -- https://master.php.net/note/delete/93057/integrated Del: useless -- https://master.php.net/note/delete/93057/useless Del: bad code -- https://master.php.net/note/delete/93057/bad+code Del: spam -- https://master.php.net/note/delete/93057/spam Del: non-english -- https://master.php.net/note/delete/93057/non-english Del: in docs -- https://master.php.net/note/delete/93057/in+docs Del: other reasons-- https://master.php.net/note/delete/93057 Reject -- https://master.php.net/note/reject/93057 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159724) next »