需要用到__autoload()方法在实例化对象的时候自动进行加载类文件
init.php //初始化文件
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/3/3 0003
* Time: 下午 3:39
*/
header('content-type:text/html;charset=utf8');
function __autoload($className){
//自动加载类名为className,文件名为./$className.php的文件
require "./$className.php";
}
2,测试
新建student类和teacher类
Student.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/3/3 0003
* Time: 下午 3:41
*/
class Student{
public function __construct()
{
echo "学生类已加载";
}
}
Teacher.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/3/3 0003
* Time: 下午 3:41
*/
class Teacher{
public function __construct()
{
echo "教师类已加载";
}
}
创建index.php文件测试
<?php
/**
* @name index.php
* @decs
* @author 老猫 <18368091722@163.com>
* Updated on: 2019/5/30 13:51
*/
header("Content-Type:text/html;charset=utf-8");
//require_once("./file_upload_object.php");
require './init.php';
$student=new Student();
echo "<br>";
$teacher=new Teacher();
?>