使用名称空间找不到PHP类

时间:2018-10-14 21:35:56

标签: php namespaces composer-php

我无法找出为什么我的课程无法加载。我正在使用Composer进行psr-4自动加载,并已成功使用它。这是我的班级设置方法:

/项目
/ classes
/ feeds
/拉
/ factory
composer.json
testMyFactory.php

feeds / factory / FeedFactory.php

namespace MyClasses\Feeds\Factory;

interface FeedFactory
{
    public function build($provider);
}

feeds / factory / PullFeedFactory.php

namespace MyClasses\Feeds\Factory;

use MyClasses\Feeds\Factory\FeedFactory;
use MyClasses\Feeds\Pull\Providers\One;

/**
 * Class FeedFactory
 */

class PullFeedFactory implements FeedFactory
{
    public function __construct(){}

    /**
     * Build provider object for factory
     * @param string $provider Type of feed provider to return
     * @return Object Provider object
     */

    public function build($provider) {
        switch ($provider) {
            case 'one':
                $provider = new One();
                break;
            default:
                $provider = new One`();
                break;
        }

        return $provider;
    }
}

project / feeds / pull / One.php

namespace MyClasses\Feeds\Pull\Providers;

class One
{
    public function pull() {
        echo 'Pull One';
    }
}

project / testMyFactory.php

require __DIR__ . '/vendor/autoload.php';

use MyClasses\Feeds\Factory\PullFeedFactory;  

$feed = new PullFeedFactory();
$feed->build('one');
$feed->pull();

project / composer.json

{
    "require": {
      //Remove for example       
    },
    "config": {
        "preferred-install": "dist"
    },
    "require-dev": {
    },
    "autoload": {
        "psr-4": {
            "MyClasses\\": "./classes",
        }
    }
}

这是我不断得到Class 'MyClasses\Feeds\Factory\PullFeedFactory' not found in /var/www/html/testPullFactory.php on line xx

的错误

我还有其他类可以通过自动加载在Classes目录中工作,但是由于某种原因无法使它正常工作。我觉得这很明显,但是已经坚持了几个小时。

更新:

已更新,包括我的vendor / autoload.php文件。 Stil出现错误,尽管现在Class 'MyClasses\Feeds\Pull\Providers\One' not found in /var/www/html/classes/Feeds/Factory/PullFeedFactory.php

有所不同

1 个答案:

答案 0 :(得分:0)

在您的composer.json中,更改为

"autoload": {
        "psr-4": {
            "MyClasses\\": "classes/",
        }
    }

然后执行composer dump-autoload