Blogger Information
Blog 25
fans 1
comment 0
visits 12908
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
自动加载器
xueblog9的进阶之旅
Original
406 people have browsed it

自动加载器

  1. 为了实现自动加载,应该遵守一些规定
    1.1 一个文件只有一个类
    1.2 这个类名和文件名必须一致
    1.3 这个类的命名空间,必须映射到类文件所在的路径
    1.4 spl_autoload_register(function($class){}),自动加载注册,当实例化一个类时,或者静态访问一个类时,当前文件没有该类时,则会调用该自动加载器,根据类名去加载,前提满足之前的三个条件,才可实现自动加载;

其他

  1. NAMESPACE:当前命名空间名
  2. call_user_func():回调,格式:类名,方法名
  3. use…as…:默认使用完全限定名称启用别名
  4. file_exists:判断文件是否存在

案例源码

  1. <?php
  2. namespace zuoye0816;
  3. use function PHPSTORM_META\sql_injection_subst;
  4. spl_autoload_register(function($class){
  5. require $class.'.php';
  6. });
  7. use test\control\test1;
  8. use test\control\test2;
  9. use test\control\test3;
  10. $testnew1 = new test1;
  11. $testnew2 = new test2;
  12. $testnew3 = new test3;
  13. echo $testnew1->test1().'<hr>';
  14. echo $testnew2->test2().'<hr>';
  15. echo $testnew3->test3().'<hr>';
  16. echo __DIR__;

类文件

案例结果

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