我对关于 php 自动加载的东西的一件事感到困惑:spl_autoload()
函数。在每个答案中,我发现此功能是__autoload
. PHP不应该自己定义默认实现,__autoload()
然后如果我明确创建__autoload()
它只会覆盖它?
如果我没有__autoload()
在我的 php 文件中明确定义函数,会有默认实现吗?是spl_autoload()
某种内部函数,如果是,那为什么在 php doc 中呢?
(如果它不是内部函数)在每个 spl_autoload() 示例中都没有对该函数的任何调用,只有spl_autoload_register
没有参数,spl_autoload_extensions
依此类推。为什么这样?我错过了什么?
引用自:什么是自动加载;如何使用 spl_autoload、__autoload 和 spl_autoload_register?
set_include_path(get_include_path().PATH_SEPARATOR.'path/to/my/directory/'); spl_autoload_extensions('.php, .inc'); spl_autoload_register();
由于 spl_autoload 是 __autoload() 魔术方法的默认实现,当您尝试实例化一个新类时,PHP 将调用 spl_autoload。
所以如果我不打电话spl_autoload_register()
,它不会注册默认实现?是否spl_autoload
查看设置的扩展名spl_autoload_extensions();
,然后从包含路径导入所有具有这些扩展名的文件?重复前面提到的问题:是spl_autoload()
内部函数吗?
我知道这__autoload()
已被弃用,我应该使用spl_autoload_register()
. 我只是想确保我知道这一切是如何运作的。
谢谢。