您的位置:首页 > 产品设计 > UI/UE

PHP 使用__autoload()方法避免频繁使用require()函数。

2017-07-03 14:50 495 查看
./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函数。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: