spl_autoload 的致命错误
Fatal error with spl_autoload
我这里有一段代码可以自动加载类:
<?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();
但我总是收到以下错误:
致命错误:spl_autoload(): Class StreamFactory 无法加载到 C:\Users\Test\PhpstormProjects\Test\test.php 的第 24 行
当我检查在包含路径中设置的路径时,它们是正确的。
有人可以给我提示,为什么会抛出该错误?
您需要将 StreamFactory
class 的名称更改为 stream_factory
并将其实例化为 $streamFactory = new stream_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();
}
classes/stream_factory.class.php
<?php
class stream_factory {
function __construct() {
echo "Hello from " . __CLASS__;
}
}
我这里有一段代码可以自动加载类:
<?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();
但我总是收到以下错误:
致命错误:spl_autoload(): Class StreamFactory 无法加载到 C:\Users\Test\PhpstormProjects\Test\test.php 的第 24 行
当我检查在包含路径中设置的路径时,它们是正确的。
有人可以给我提示,为什么会抛出该错误?
您需要将 StreamFactory
class 的名称更改为 stream_factory
并将其实例化为 $streamFactory = new stream_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();
}
classes/stream_factory.class.php
<?php
class stream_factory {
function __construct() {
echo "Hello from " . __CLASS__;
}
}