Blogger Information
Blog 31
fans 0
comment 0
visits 14254
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
封装多个类,并在客户端完成类的自动加载。
木子木杉
Original
384 people have browsed it

封装类

类名与文件名相同,类是以class开头,类名首字母必须大写
成员属性要有访问修饰符
构造函数 类每实例化一次 自动被调用
成员方法

引用类文件过多,调用类的自动加载器
autoload.php

  1. spl_autoload_register(function ($className) {
  2. printf('类名:%s<br>', $className);
  3. // var_dump(__DIR__);
  4. $file = __DIR__ . DIRECTORY_SEPARATOR . $className . '.php';
  5. if (!is_file($file) && file_exists(($file))) {
  6. throw new \Exception('文件不合法或者文件不存在');
  7. }
  8. require $file;
  9. });

在客户端 引用类的自动加载器
require ‘autoload.php’;
new关键字完成类的实例化

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post