./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();
?>_autoload()是php中的一个魔术方法,在代码中当调用不存在的类时会自动调用该方法。
使用该__autoload()方法就可以避免调用其他类时,频繁使用require函数。
本文介绍了PHP中的魔术方法__autoload的工作原理及其使用方式。通过实例演示了如何利用此方法自动加载类文件,从而避免显式使用require函数。适用于希望简化代码结构并提高开发效率的PHP开发者。
959

被折叠的 条评论
为什么被折叠?



