【发布时间】:2019-05-16 07:36:44
【问题描述】:
我在通过 composer 使用自动加载的类时遇到问题。
这是我的文件结构:
-cache
-public
-src
--Repository
---PostRepository.php
-index.php
-composer.json
我正在尝试在 index.php 中加载 PostRepository.php。 但是报错
致命错误:未捕获的错误:类 'App\Repository\PostRepository' 不是 在 /opt/lampp/htdocs/index.php:12 中找到堆栈跟踪:#0 {main} throw 在第 12 行的 /opt/lampp/htdocs/index.php 中
这是我的 composer.json:
{
"require": {
"twig/twig": "^2.0"
},
"autoload": {
"psr-4": {
"App\\" : "src/"
}
}
}
这是我的 PostRepository.php
<?php
namespace App\Repository;
class PostRepository
{
...
}
这是我的 index.php
<?php
require __DIR__ . "/vendor/autoload.php";
use App\Repository\PostRepository;
$postClass = new PostRepository();
$posts = $postClass->getAllPosts();
var_dump($posts);
看起来命名空间没问题。有什么问题?
【问题讨论】:
-
根据您的粘贴,第 12 行出现错误。但是发布的 index.php 只有 9 行。
标签: php