dongyuji7309 2018-08-04 20:18
浏览 83
已采纳

PHP删除自动加载器后,Composer失败了

I am following Lopez' "Learning PHP 7" book and in Ch. 5 on MVC I would be more than grateful to learn what is wrong here.

When in init.php I comment out - see the nine-line comment in init.php - the autoloader function, which should not be needed any more, Lopez writes, I get this error:

PHP Fatal error: Uncaught Error: Class 'Bookstore\Utils\Config' not found in /home/petr/Documents/workspace/bookstore/init.php:25 Stack trace: ...

Line 25 in init.php which is shown below entirely is

$dbConfig = Config::getInstance()->get('db');

(When installing PHP, MySQL, Composer, and Twig I did not use Vagrant [had installed PHP, MySQL, and Apache on Ubuntu before starting with this book and have added Composer w. Twig via Synaptic today]. I use PHP's default web server developing this project.)

The project structure is like this:

enter image description here

init.php is like so:

<?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 is this file:

{
  "require": {
    "monolog/monolog": "^1.17",
    "twig/twig": "^1.23"
    },
  "autoload": {
    "psr-4": {
      "Bookstore\\": "bookstore"
      }
    }
}

And this is the 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];

  }

}

I wonder how to make the project work with the function autoloader removed from init.php. Thank you.

  • 写回答

1条回答 默认 最新

  • duanlun4411 2018-08-04 21:25
    关注

    You need an autoloader to magically load the classes. As you have defined composer.json with your dependencies and your PSR-4 autoloading rule, I would recommend to use Composers autoloader.

    Your composer.json configuration has a small error: The PSR-4 autoloader tries to load the classes within the Bookstore namespace from a sub-folder called bookstore. These classes are placed in your project's root directory. Therefore the autoloader has to point to that directory instead by leaving the directory path empty:

    {
      "require": {
        "monolog/monolog": "^1.17",
        "twig/twig": "^1.23"
        },
      "autoload": {
        "psr-4": {
          "Bookstore\\": ""
          }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器