传统上,在PHP里,当我们要用到一个class文件的时候,我们都得在文档头部require或者include一下:
<"htmlcode"><"./{$class_name}.php"; }对,可以使用PHP的魔法函数__autoload(),上面的示例就是自动加载当前目录下的PHP文件。当然,实际当中,我们更可能会这么来使用:
<"../includes/{$name}.php"; if(file_exists($path)){ require_once($path); }else{ die("the file {$class_name} could not be found"); } }也即是做了一定的文件名大小写处理,然后在require之前检查文件是否存在,不存在的话显示自定义的信息。
类似用法经常在私人项目,或者说是单一项目的框架中见到,为什么呢?因为你只能定义一个__autoload function,在多人开发中,做不到不同的developer使用不同的自定义的autoloader,除非大家都提前说好了,都使用一个__autoload,涉及到改动了就进行版本同步,这很麻烦。
也主要是因为此,有个好消息,就是这个__autoload函数马上要在7.2版本的PHP中弃用了。
Warning This feature has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged.
那么取而代之的是一个叫spl_autoload_register()的东东,它的好处是可以自定义多个autoloader.//使用匿名函数来autoload spl_autoload_register(function($class_name){ require_once('...'); });//使用一个全局函数 function Custom() { require_once('...'); } spl_autoload_register('Custom');//使用一个class当中的static方法 class MyCustomAutoloader { static public function myLoader($class_name) { require_once('...'); } } //传array进来,第一个是class名,第二个是方法名 spl_autoload_register(['MyCustomAutoloader','myLoader']);//甚至也可以用在实例化的object上 class MyCustomAutoloader { public function myLoader($class_name) { } } $object = new MyCustomAutoloader; spl_autoload_register([$object,'myLoader']);值得一提的是,使用autoload,无论是__autoload(),还是spl_autoload_register(),相比于require或include,好处就是autoload机制是lazy loading,也即是并不是你一运行就给你调用所有的那些文件,而是只有你用到了哪个,比如说new了哪个文件以后,才会通过autoload机制去加载相应文件。
当然,laravel包括各个package里也是经常用到spl_autoload_register,比如这里:
/** * Prepend the load method to the auto-loader stack. * * @return void */ protected function prependToLoaderStack() { spl_autoload_register([$this, 'load'], true, true); }
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 黑神话悟空上品鼠弩手精魄获取方法一览|上品鼠弩手精魄收集攻略
- 中国音乐地图听见世界非物质文化遗产《非遗HQCD》wav
- 中国民乐合奏作品集《华夏之音HQ》头版限量编号[WAV+CUE]
- [电影原声带]黎允文《天将雄师》[FLAC+CUE]
- 曝NS继任机型已进入量产阶段:已花了30亿日元
- 不是Switch2?曝《马里奥惊奇》NS捆绑包即将公开
- 暖心任天堂!网友Switch维修返还时贴纸被完好保留
- 《国语老歌 经典对唱情歌 2CD》[WAV/分轨][1.1GB]
- 《刀郎 翻唱精选华语专辑 披着羊皮的狼 》[WAV+CUE][430MB]
- 《真的田震 中国乐坛红极一时的歌者 精品集》[WAV+CUE][300MB]
- 群星.1995-新滚石九大天王之情歌大全【滚石】【WAV+CUE】
- 群星.1993-滚石九大天王十二出好戏·纵夏欢唱【滚石】【WAV+CUE】
- 群星.1993-滚石九大天王十二出好戏·贺岁齐唱【滚石】【WAV+CUE】
- 黑神话悟空上品虫校尉精魄获取方法一览|上品虫校尉精魄收集攻略
- 《指环王:夏尔的传说》推迟至2025年初发行