NameScape错误。陷入' MyApp \ Chat'

时间:2017-08-14 11:19:41

标签: php composer-php ratchet

棘轮中的错误简单示例:

Fatal error: Uncaught Error: Class 'MyApp\Chat' not found

我的文件结构如下:

root\composer.json 
root\bin\chat-server.php 
root\src\MyApp\Chat.php

Composer.json是

{
    "autoload": {
        "psr-0": {
            "MyApp": "src"
        }
    },
    "require": {
        "cboden/ratchet": "0.3.*"
    }
}

chat-server.php是

<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;

    require dirname(__DIR__) . '/vendor/autoload.php';

    $server = IoServer::factory(
        new Chat(),
        8080
    );

    $server->run();

和Chat.php是

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    public function onOpen(ConnectionInterface $conn) {
    }

    public function onMessage(ConnectionInterface $from, $msg) {
    }

    public function onClose(ConnectionInterface $conn) {
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}

我试过以下,但现在仍然有效: PHP Fatal error: Class 'MyApp\Chat' not found in /MyApp/chat-server.php

1 个答案:

答案 0 :(得分:3)

检查您的自动加载命名https://getcomposer.org/doc/04-schema.md#psr-0

应该是这样的:

{
    "autoload": {
        "psr-0": {
            "MyApp\\": "src/"
        }
    },
    "require": {
        "cboden/ratchet": "0.3.*"
    }
}

并且

composer dump-autoload