note 107835 added to function.autoload
| From: | qeremy | Date: | Thu, 08 Mar 2012 15:01:15 +0000 |
| Subject: | note 107835 added to function.autoload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186545@lists.php.net to get a copy of this message | ||
Even I have never been using this function, just a simple example in order to explain it;
./myClass.php
<?php
class myClass {
public function __construct() {
echo "myClass init'ed successfuly!!!";
}
}
?>
./index.php
<?php
// we've writen this code where we need
function __autoload($classname) {
$filename = "./". $classname .".php";
include_once($filename);
}
// we've called a class ***
$obj = new myClass();
?>
*** At this line, our "./myClass.php" will be included! This is the magic that we're
wondering... And you get this result "myClass init'ed successfuly!!!".
So, if you call a class that named as myClass then a file will be included myClass.php if it exists
(if not you get an include error normally). If you call Foo, Foo.php will be included, and so on...
And you don't need some code like this anymore;
<?php
include_once("./myClass.php");
include_once("./myFoo.php");
include_once("./myBar.php");
$obj = new myClass();
$foo = new Foo();
$bar = new Bar();
?>
Your class files will be included "automatically" when you call (init) them without these
functions: "include, include_once, require, require_once".
----
Server IP: 69.147.83.197
Probable Submitter: 194.27.149.90
----
Manual Page -- http://www.php.net/manual/en/function.autoload.php
Edit -- https://master.php.net/note/edit/107835
Del: integrated -- https://master.php.net/note/delete/107835/integrated
Del: useless -- https://master.php.net/note/delete/107835/useless
Del: bad code -- https://master.php.net/note/delete/107835/bad+code
Del: spam -- https://master.php.net/note/delete/107835/spam
Del: non-english -- https://master.php.net/note/delete/107835/non-english
Del: in docs -- https://master.php.net/note/delete/107835/in+docs
Del: other reasons-- https://master.php.net/note/delete/107835
Reject -- https://master.php.net/note/reject/107835
Search -- https://master.php.net/manage/user-notes.php