百科狗-知识改变命运!
--

spl_autoload() - SPL函数

梵高12个月前 (11-21)阅读数 31#技术干货
文章标签函数

spl_autoload()

(PHP 5 >= 5.1.0, PHP 7)

__autoload()函数的默认实现

说明

spl_autoload(string $class_name[,string $file_extensions]): void

本函数提供了__autoload()的一个默认实现。如果不使用任何参数调用 spl_autoload_register()函数,则以后在进行 __autoload()调用时会自动使用此函数。

参数

$class_name$file_extensions

spl_autoload() - SPL函数

在默认情况下,本函数先将类名转换成小写,再在小写的类名后加上.inc 或.php 的扩展名作为文件名,然后在所有的包含路径(include paths)中检查是否存在该文件。

返回值

没有返回值。

Note, that the default autoload implementation is written in C land and is always slightly faster then your native PHP one.
Here is a trick to use default implementation with any configuration:

This also works with namespaces out of the box. So you can write code like "use My\Name\Object" and it will map to "class/My/Name/Object.class.php" file path!
If you want to make the best use out of autoload with an APC cache don't use spl_autoload. It uses relative paths and thus will perform a stat even with apc.stat=0 (either that, or it doesn't work at all).
Instead make a custom function and use require/include with an absolute path (register it with spl_autoload_register).
Do NOT use *_once functions or a relative path. This will fail harder than spl_autoload.
Also avoid using file_exists and is_file. This will also perform a stat.
Why are stats bad? Because they access the file system. PHP does have a stat cache that helps, but it defeats the purpose of apc.stat = 0.
It's also good to keep in mind that it's good to keep your custom autoload function simple. This is my Loader class:

ClassB.php

ClassC.php

ClassD.php

ClassE.php

1. Simple:

2. Change priority:

Or you can use this simple function that runs a bit faster for the extensions with lower priority :)

---
Safak Ozpinar - Istanbul University, Computer Engineering
Note this function will LOWERCASE the class names its looking for, dont be confused when it cant find Foo_Bar.php
also, unlike most other autoloader code snippets, this function DOES NOT translate underscores to slashes.
class Foo_Bar {}
will load foo_bar.php and will not try to load foo/bar.php
You can get around this with
spl_autoload_register(function($class) { return spl_autoload(str_replace('_', '/', $class));});
One small example that shows how you can use spl_autoload function in your MVC, Framewrk's applications. For example, will use the Loader class. 
Just thought I'd react to simast at gmail dot com's note: While he has a point in saying C outperforms PHP, his suggestion is micro-optimization. I'm not 100% against micro-optimizing code, but if you do, go all the way:

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)