PHP的类自动加载实例

Original 2019-05-13 15:19:04 164
abstract:学生类Student<?phpclass Student{    public $name;    public $age;    public $sex;    public function __construct($name,$age,$sex)    {     &
  1. 学生类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;
       }
    }

  2. 老师类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;
       }
    }

  3. 类的自动加载文件

    <?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>';

Correcting teacher:查无此人Correction time:2019-05-14 09:32:41
Teacher's summary:完成的不错。类学完后,相当于php入门了。继续加油。

Release Notes

Popular Entries