我正在关注 Lopez 的“Learning PHP 7”一书和 Ch。 5 关于 MVC 我会非常感谢了解这里的错误。
当我在 init.php 中注释掉时 - 请参阅 init.php 中的九行注释 - 自动加载器功能,应该不再需要了,洛佩兹写道,我收到了这个错误:
PHP Fatal error: Uncaught Error: Class 'Bookstore\Utils\Config' not found in /home/petr/Documents/workspace/bookstore/init.php:25 Stack trace: ...
init.php 中的第 25 行完全如下所示
$dbConfig = Config::getInstance()->get('db');
(在安装 PHP、MySQL、Composer 和 Twig 时,我没有使用 Vagrant [在开始阅读本书之前已经在 Ubuntu 上安装了 PHP、MySQL 和 Apache,并且今天通过 Synaptic 添加了 Composer w. Twig]。我使用 PHP 的默认 Web 服务器开发这个项目。)
项目结构是这样的:
init.php 是这样的:
<?php
use Bookstore\Domain\Customer\Basic;
use Bookstore\Domain\Customer\Premium;
use Bookstore\Domain\Customer\CustomerFactory;
use Bookstore\Domain\Customer;
use Bookstore\Domain\Payer;
use Bookstore\Domain\Person;
use Bookstore\Domain\Book;
use Bookstore\Utils\Config;
use Bookstore\Utils\Unique;
use Bookstore\Exceptions\InvalidIdException;
use Bookstore\Exceptions\ExceededMaxAllowedException;
// function autoloader($classname) {
// $lastSlash = strpos($classname, '\\') + 1;
// $classname = substr($classname, $lastSlash);
// $directory = str_replace('\\', '/', $classname);
// $filename = __DIR__ . '/' . $directory . '.php';
// require_once($filename);
// }
//
// spl_autoload_register('autoloader');
$dbConfig = Config::getInstance()->get('db');
$db = new PDO(
'mysql:host=127.0.0.1;dbname=bookstore',
$dbConfig['user'],
$dbConfig['password']
);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
function addBook(int $id, int $amount = 1): void {
$db = new PDO(
'mysql:host=127.0.0.1;dbname=bookstore',
'root',
''
);
$query = 'UPDATE book SET stock = stock + :n WHERE id = :id';
$statement = $db->prepare($query);
$statement->bindValue('id', $id);
$statement->bindValue('n', $amount);
if (!$statement->execute()) {
throw new Exception($statement->errorInfo()[2]);
}
}
function addSale(int $userId, array $bookIds): void {
$db = new PDO(
'mysql:host=127.0.0.1;dbname=bookstore',
'root',
''
);
$db->beginTransaction();
try {
$query = 'INSERT INTO sale (customer_id, date) '
. 'VALUES(:id, NOW())';
$statement = $db->prepare($query);
if (!$statement->execute(['id' => $userId])) {
throw new Exception($statement->errorInfo()[2]);
}
$saleId = $db->lastInsertId();
$query = 'INSERT INTO sale_book (book_id, sale_id) '
. 'VALUES (:book, :sale)';
$statement = $db->prepare($query);
$statement->bindValue('sale', $saleId);
foreach ($bookIds as $bookId) {
$statement->bindValue('book', $bookId);
if (!$statement->execute()) {
throw new Exception($statement->errorInfo()[2]);
}
}
$db->commit();
}
catch (Exception $e) {
$db->rollBack();
throw $e;
}
}
try {
addSale(1, [3, 7]);
}
catch (Exception $e) {
echo 'Error adding sale: ' . $e->getMessage();
}
composer.json 是这个文件:
{
"require": {
"monolog/monolog": "^1.17",
"twig/twig": "^1.23"
},
"autoload": {
"psr-4": {
"Bookstore\\": "bookstore"
}
}
}
这是 Config.php:
<?php
namespace Bookstore\Utils;
use Bookstore\Exceptions\NotFoundException;
class Config {
private static $data; // private $data;
private static $instance;
private function __construct() { // public function __construct() {
$json = file_get_contents(__DIR__ . '/../config/app.json');
self::$data = json_decode($json, true); // $this->data = json_decode($json, true);
}
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new Config();
}
return self::$instance;
}
public static function get($key) { // public function get($key) {
if (!isset(self::$data[$key])) { // if (!isset($this->data[$key])) {
throw new NotFoundException("Key $key not in config.");
}
return self::$data[$key]; // return $this->data[$key];
}
}
我想知道如何使项目与功能一起工作
autoloader
从 init.php 中删除。谢谢你。
最佳答案
您需要一个自动加载器来神奇地加载类。正如您所定义的 composer.json
根据您的依赖项和 PSR-4 自动加载规则,我建议使用 Composers 自动加载器。
您的 composer.json
配置有一个小错误:PSR-4 自动加载器尝试加载 Bookstore
中的类来自名为 bookstore
的子文件夹的命名空间.这些类放置在项目的根目录中。因此,自动加载器必须指向该目录,而不是将目录路径留空:
{
"require": {
"monolog/monolog": "^1.17",
"twig/twig": "^1.23"
},
"autoload": {
"psr-4": {
"Bookstore\\": ""
}
}
}
关于PHP 在删除自动加载器时 Composer 使工作失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51689273/