i不能在名称空间中自动加载class的父。没有继承子班也加载;但是孩子无法自动加载父级。
文件结构:
/index.php
/lib/router.php
/lib/ns1/parent1.php
/lib/ns1/child1.php
index.php:
spl_autoload_extensions(".php");
spl_autoload_register();
require '/lib/router.php';
/lib/router.php
$child1 = new ns1child1();
/lib/ns1/child1.php
namespace ns1;
class child1 extends parent1 {}
/lib/ns1/parent1.php
namespace ns1;
class parent1 { function __construct() {} }
当我从child1中删除"扩展"零件时,一切正常。使用"扩展"部分,我有错误:
致命错误:spl_autoload():class parent1 child1不能在/lib/ns1/child1.php
中加载
有什么办法如何使用内置的默认SPL自动加载函数来做到这一点?非常感谢您的帮助。
我找到了在此答案中适合我的解决方案https://stackoverflow.com/a/7987085/5208203
只需将set_include_path()添加到文件index.php:
set_include_path(get_include_path().PATH_SEPARATOR."/lib");
spl_autoload_extensions(".php");
spl_autoload_register();
require '/lib/router.php';
但我不确定性能,可能只是我在课堂定义中遇到的另一个失败的方法...