本文主要是介绍类自动加载:spl_autoload_register(函数名传递地址或者匿名函数),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<?php
//引入的类文件:并命名为:Student.class.php
header('Content-type:text/html;charset=utf-8');
final class Student{const TILTLE = "3班";private $name = "李立";private $age = 20;public function __construct(){echo "{$this->name}的年龄是{$this->age}";}public function ShowInfo(){echo "{$this->name}的班级是".self::TILTLE;}
}
<?php
//执行加载的文件
header('Content-type:text/html;charset=utf-8');
//第一种:函数名传递
//spl_autoload_register(参数是字符串的函数名称,函数传递地址)
spl_autoload_register('fun1');
function fun1($ClassName){$filename = "./$ClassName.class.php";if (file_exists($filename)) {require_once($filename);}
}
/*//第二种:采用匿名函数传递
//spl_autoload_register()参数是匿名函数
spl_autoload_register(function($ClassName){$filename = "./$ClassName.class.php";if (file_exists($filename)) {require_once($filename);}
});*/
//创建对象
$obj = new Student();//创建引入的类文件的对象;
$obj->ShowInfo();//调用引入的类文件的方法;
这篇关于类自动加载:spl_autoload_register(函数名传递地址或者匿名函数)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!