Php spl_自动加载时出现致命错误
我这里有一段自动加载类的代码:Php spl_自动加载时出现致命错误,php,spl-autoload-register,spl-autoload-call,Php,Spl Autoload Register,Spl Autoload Call,我这里有一段自动加载类的代码: <?php $test = [ 'includeDirs' => [ 'interfacesDir' => __DIR__ . DIRECTORY_SEPARATOR . 'interfaces'. DIRECTORY_SEPARATOR, 'abstractsDir' => __DIR__ . DIRECTORY_SEPARATOR . 'abstra
<?php
$test = [
'includeDirs' => [
'interfacesDir' => __DIR__ . DIRECTORY_SEPARATOR . 'interfaces'. DIRECTORY_SEPARATOR,
'abstractsDir' => __DIR__ . DIRECTORY_SEPARATOR . 'abstracts'. DIRECTORY_SEPARATOR,
'classesDir' => __DIR__ . DIRECTORY_SEPARATOR . 'classes'. DIRECTORY_SEPARATOR
],
'includeExtensions' => [
'classExtension' => '.class.php',
'abstractExtension' => '.abstract.php',
'interfaceExtension' => '.interface.php'
]
];
set_include_path('.');
set_include_path(get_include_path() . PATH_SEPARATOR . implode(PATH_SEPARATOR, $test['includeDirs']));
spl_autoload_extensions(implode(',', $test['includeExtensions']));
spl_autoload_register();
$streamFactory = new StreamFactory();
您需要将StreamFactory
类的名称更改为stream\u factory
,并使用$StreamFactory=new stream\u factory()实例化它
或将文件'stream_factory.class.php'重命名为StreamFactory.class.php
index.php
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$test = [
'includeDirs' => [
'interfacesDir' => __DIR__ . DIRECTORY_SEPARATOR . 'interfaces'. DIRECTORY_SEPARATOR,
'abstractsDir' => __DIR__ . DIRECTORY_SEPARATOR . 'abstracts'. DIRECTORY_SEPARATOR,
'classesDir' => __DIR__ . DIRECTORY_SEPARATOR . 'classes'. DIRECTORY_SEPARATOR
],
'includeExtensions' => [
'classExtension' => '.class.php',
'abstractExtension' => '.abstract.php',
'interfaceExtension' => '.interface.php'
]
];
set_include_path('.');
set_include_path(get_include_path() . PATH_SEPARATOR . implode(PATH_SEPARATOR, $test['includeDirs']));
spl_autoload_extensions(implode(',', $test['includeExtensions']));
spl_autoload_register();
try {
$streamFactory = new \stream_factory();
} catch(Exception $e) {
echo $e->getMessage();
}
<?php
class stream_factory {
function __construct() {
echo "Hello from " . __CLASS__;
}
}
StreamFactory类的位置在哪里?它是classes
?是的,它在这里:C:\Users\Test\PhpstormProjects\Test\classes\stream\u factory.class.php还有一个问题,StreamFactory使用的名称空间是什么?我没有设置任何名称空间。它是全局的。您是否尝试过$streamFactory=new\streamFactory()代码>?