学生类Student
<?php
class Student
{
public $name;
public $age;
public $sex;
public function __construct($name,$age,$sex)
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
}
老师类Teacher
<?php
class Teacher
{
public $name;
public $age;
public $sex;
public function __construct($name,$age,$sex)
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
}
类的自动加载文件
<?php
//类的自动加载器:最重要的一个参数就是一个回调
spl_autoload_register(function($className){
require __DIR__ . '\public\\' . $className . '.php';
});
$student = new Student('李明',12,'男');
$teacher = new Teacher('朱老师',30,'男');
echo '学生:' . $student->name . '的年龄是: ' . $student->age . ',性别是: ' . $student->sex . '<br>';
echo '老师:' . $teacher->name . '的年龄是: ' . $teacher->age . ',性别是: ' . $teacher->sex . '<br>';