返回PHP的类自动......登陆

PHP的类自动加载实例

连界 现代 周伟2019-05-13 15:19:04164
  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>';

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送