Blogger Information
Blog 50
fans 0
comment 0
visits 31579
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
自动加载器
手机用户1580651468
Original
472 people have browsed it

自动加载器

一、封装类的关键代码实现

1.新建一个类autoLoad

  1. <?php
  2. namespace auto;
  3. class autoLoad{
  4. public static function load($class)
  5. {
  6. // 类名到类文件名
  7. // 1.将路径分隔符替换
  8. $path = str_replace('\\', DIRECTORY_SEPARATOR, $class);
  9. $file = __DIR__ . '/' . $path . '.php';
  10. if ( is_file($file) && file_exists($file))
  11. {require $file;}
  12. else {die($file . '不存在');}
  13. }
  14. }

2.调用类的代码

  1. <?php
  2. include "autoLoad.php";
  3. spl_autoload_register(["auto\autoLoad","load"]);
  4. use admin\controller\Index;
  5. use admin\model\User;
  6. use admin\view\index\Hello;
  7. $Index = new Index();
  8. $User = new User();
  9. $Hello = new Hello();
  10. echo Index::show() . PHP_EOL;
  11. echo User::show() . PHP_EOL;
  12. echo Hello::show() . PHP_EOL;

3.新建的三个类名(代码略)直接上图

二、最后的效果图

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