我在[root]/composer.json
文件中有以下类自动加载定义:
{
...
"autoload": {
"psr-0": {
"": [
"application/models",
"application/controllers",
"application/forms",
"library/"
]
},
"psr-4": {
"": ["src/"]
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
...
}
当我调用[root]/public_html/index.php
页面时,出现以下错误:
PHP致命错误:未捕获错误:类'类\ DependencyInjection'在/var/www/html/application/bootstrap.php:29
中找不到
[root]/public_html/index.php
中的内容是以下代码:
$bootstrap = true;
require_once '../application/bootstrap.php';
[root]/application/bootstrap.php
文件中的内容是:
// turn on autoloading for classes
// composer autoloader
include(MMIPATH.'/vendor/autoload.php');
// zend autoload
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
$diContainer = new classes\DependencyInjection(services.yaml');
$proxy = $diContainer->get('containerProxy');
这是[root]/library/classes/DependencyInjection.php
:
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
USE Symfony\Component\DependencyInjection\Container;
class DependencyInjection extends ContainerBuilder
{
....
}
这里有什么问题?为什么自动加载器无法找到该类?
答案 0 :(得分:0)
您正在尝试加载“类”命名空间,但是您的类未被定义为位于“classes”命名空间中。
PSR-0中的 new classes\DependencyInjection(...)
加载{paths}\classes\DependencyInjection.php
并尝试从名称空间DependencyInjection
实例化类classes
,但DependencyInjection
不在classes
中1}}命名空间。该文件将加载但该类不存在。
您可以为每个类添加namespace classes;
,但这不是一个好的解决方案。更好的解决方案是使用正确的命名空间或更改PSR-0列表以包含库/类并使用new DependencyInjection(...)
。 (我的投票是针对第一个 - 使用适当的命名空间。)
根据要求。例如:
文件位置
{app}\library\UsefullNamespace\DependencyInjection.php
使用它
new UsefullNamespace\DependencyInjection.php
DependencyInjection.php:
namespace UsefullNamespace;
use [...];
class DependencyInjection extends ContainerBuilder
{