【问题标题】:PHP autoloader not working with namespacesPHP自动加载器不适用于命名空间
【发布时间】:2016-04-08 20:22:26
【问题描述】:

这是第一次使用自动加载器并遇到一些错误。 结构如下:

  • AnimalShop(根)
      • Shop.php
    • index.php

我有以下简单的代码

Index.php

<?php

spl_autoload_register(function ($class_name) 
{
    include $class_name . '.php';
});

echo "<h1>PETs SHOP</h1>";

// Create a shop

$shop = new Shop();

Shop 是一个简单的类

<?php

namespace PHPAdvanced\AnimalShop\classes;

/*
 * Pet shop
 */

    class Shop
    {

        /**
         * @var Pets[] pets
         */
        private $pets = [];

        public function addPetsToArray(Pets $pet)
        {
            $this->pets[] = $pet;
        }


        /**
         * Print pets naam
         */
        public function printPets()
        {
            foreach($this->pets as $pet)
            {
                echo "<p>" . $pet->getPetNaam() . "</p>";
            }
        }
    }

当我运行 index.php 时出现以下错误:

警告:include(Shop.php):无法打开流:第 4 行的 /var/www/ppadvancedCourse/AnimalShop/index.php 中没有这样的文件或目录

警告:include():无法在第 4 行的 /var/www/ppadvancedCourse/AnimalShop/index.php 中打开“Shop.php”以包含 (include_path='.:/usr/share/php:')

【问题讨论】:

  • shop.php 位于/var/www/phpadvancedCourse/AnimalShop/ 吗?
  • nvm 我在你的问题中看到了..
  • include 'classes/' . $class_name . '.php';
  • 顺便说一句,这是一种使用命名空间和自动加载器的奇怪方式。你应该看看PSR-0PSR-4

标签: php namespaces autoload


【解决方案1】:
spl_autoload_register(function ($class_name) 
{
    include realpath(dirname(__FILE__))."/classes/".$class_name . '.php';
});

你的路径错误..试试这个..

【讨论】:

  • 上面的代码不起作用我得到的错误是:致命错误:未捕获的错误:在第 12 行的 /var/www/phpadvancedCourse/AnimalShop/index.php 中找不到类“商店”错误:类在第 12 行的 /var/www/phpadvancedCourse/AnimalShop/index.php 中找不到“商店”
  • 如果这是您遇到的错误,那么您没有使用我的代码。在说它不起作用之前,您为什么不实际尝试一下我给您的代码。
【解决方案2】:

为了解决这个问题,我通过 composer 使用了 PSR-4 自动加载,具有以下结构和代码。

结构

  • AnimalShop(根)
    • index.php
    • 应用程序(文件夹)
      • 行为(文件夹)
        • WalkBehavior.php(界面)
      • 宠物(文件夹)
        • Cat.php
        • 狗.php
        • Fish.php
        • Pets.php(抽象类)
      • 商店(文件夹)
        • PetShop.php

Composer.json

{
    "autoload" : {
        "psr-4" : {
            "App\\" : "App"
        }
    }
}

index.php

<?php

// Autoload
require "vendor/autoload.php";

use App\Shop\PetShop;
use App\Pets\Dog;
use App\Pets\Fish;
use App\Pets\Cat;

echo "<h1>PETs SHOP</h1>";

// Create a shop
$shop = new PetShop();

$shop->addPetsToArray(new Dog("Yuki"));
$shop->addPetsToArray(new Fish("BLubie"));
$shop->addPetsToArray(new Cat("Cattie"));

$shop->printPets();

宠物店和狗的例子

<?php

namespace App\Shop;

use App\Pets\Pets;

/*
 * Pet shop
 */

class PetShop
{

    /**
     * @var Pets[] pets
     */
    private $pets = [];

    public function addPetsToArray(Pets $pet)
    {
        $this->pets[] = $pet;
    }


    /**
     * Print pets naam
     */
    public function printPets()
    {
        foreach($this->pets as $pet)
        {
            echo "<p>" . $pet->getPetNaam() . "</p>";
        }
    }
}

<?php
namespace App\Pets;

/**
 * Class Dog
 */
class Dog extends Pets
{
    /**
     * Dog constructor.
     *
     * @param $name
     */
    public function __construct(string $name)
    {
        parent::__construct($name);
    }

    public function walk() : string
    {
        return "Dog is walking";
    }
}

【讨论】:

    猜你喜欢
    • 2011-09-18
    • 2013-10-24
    • 2013-11-25
    • 2014-04-13
    • 2021-06-29
    • 2011-08-06
    • 2015-07-15
    相关资源
    最近更新 更多