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".
__autoload
(Информация о версии неизвестна, возможно, только в SVN)
__autoload — Attempt to load undefined class
Описание
void __autoload
( string
$class
)You can define this function to enable classes autoloading.
Список параметров
-
class -
Name of the class to load
Возвращаемые значения
Эта функция не возвращает значения после выполнения.
Смотрите также
- spl_autoload_register() - Регистрирует заданную функцию в качестве реализации метода __autoload()
qeremy ¶
1 year ago
keyboardSmasher ¶
1 month ago
qeremy, your code is incorrect.
<?php
include_once("./myClass.php");
include_once("./myFoo.php");
include_once("./myBar.php");
$obj = new myClass();
$foo = new Foo();
$bar = new Bar();
?>
<?php
$foo = new Foo();
$bar = new Bar();
?>
should be:
<?php
$foo = new myFoo();
$bar = new myBar();
?>
