【问题标题】:Class not found, using include_once in namespace找不到类,在命名空间中使用 include_once
【发布时间】:2015-03-03 11:05:42
【问题描述】:

我试图在我的命名空间中使用我的一个类,但我得到一个类未找到错误:

PHP Fatal error:  Class 'ChatManager' not found in /var/www/soFitTest/chat/src/MyApp/Chat.php on line 17

代码如下:

namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

include_once __DIR__.'/ChatMananger.php';


    class Chat implements MessageComponentInterface 
    {
        protected $clients;

        public function __construct() {
            $this->clients = new \SplObjectStorage;
            echo "new server is running";
            $chatManager = new \ChatMananger(1, 1);
        }
    }

我的文件结构是:

我在页面顶部发布的代码在Chat.php中找到

ChatManangerChatMananger.php中找到

ChatMananger.php 文件:

<?php

require_once 'DBConnection.php';

    class ChatMananger
    {
         const DEFAULT_CHAT_SIZE = 5;
         const DEFAULT_CHAT_TYPE = 1;

         private $dbConnection;
         private $chatId;
         private $senderId;
         private $receiverId;
         private $type = ChatManager::DEFAULT_CHAT_TYPE;

          public function __construct($senderId, $receiverId)
          {   
           $this->dbConnection = DBConnection::getDBConnection();  

           $this->senderId = $senderId;
           $this->receiverId = $receiverId;     
           }

        public function getDBConnection()
        {
                return $this->dbConnection; 
        }
    }

编辑

我在__construct 中处理了一些事情

<?php

namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

include_once __DIR__.'/ChatMananger.php';
include_once __DIR__.'/ChatConsts.php';
require_once '/var/www/libs/DBConnection.php';

class Chat implements MessageComponentInterface 
{
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
        echo "new server is running";
        new ChatMananger(1, 1);
    }

    public function onOpen(ConnectionInterface $conn)
    {
        // Store the new connection to send messages to later

        $querystring = $conn->WebSocket->request->getQuery();

        foreach ($querystring as $key => $value)
        {
            //echo PHP_EOL."key ".$key." value ".$value;

            if($key == "senderId")
                $conn->senderId = $value;
            else if($key == "receiverId")
                $conn->receiverId = $value;
        } 

        $chatManager = new ChatMananger($conn->senderId, $conn->receiverId);

        $conn->chatId = $chatManager->getChatId();

        $this->clients->attach($conn, $conn->chatId);

        echo PHP_EOL."New connection! ({$conn->resourceId}) chatId ({$conn->chatId})";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }

}

ChatMananger.php

namespace MyApp;

require_once '/var/www/libs/DBConnection.php';

class ChatMananger
{
     const DEFAULT_CHAT_SIZE = 5;
     const DEFAULT_CHAT_TYPE = 1;

     private $dbConnection;
     private $chatId;
     private $senderId;
     private $receiverId;
     private $type = self::DEFAULT_CHAT_TYPE;

      public function __construct($senderId, $receiverId)
      {   
       $this->dbConnection = \DBConnection::getDBConnection();  

       $this->senderId = $senderId;
       $this->receiverId = $receiverId;     
       }

    public function getDBConnection()
    {
            return $this->dbConnection; 
    }
}

现在我的问题是在方法onOpen我使用:

$chatManager = new ChatMananger($conn->senderId, $conn->receiverId);

对于此代码,我收到此错误:

PHP Fatal error:  Class 'ChatMananger' not found in /var/www/soFitTest/chat/src/MyApp/Chat.php on line 37

【问题讨论】:

  • 你可以添加你的 ChatManager 的内容吗?

标签: php namespaces


【解决方案1】:

首先导入 ChatManager (Using namespaces in PHP):

include_once __DIR__.'/ChatMananger.php';

use MyApp\ChatManager;

然后像这样使用它:

$chatManager = new ChatMananger(1, 1);

【讨论】:

  • 我收到此错误:PHP 致命错误:无法在第 6 行的 /var/www/soFitTest/chat/src/MyApp/ChatMananger.php 中重新声明类 ChatMananger
  • 在您的 ChatManager 类中声明命名空间 MyApp:
  • 给类添加命名空间,解决了,谢谢!
  • 我没有看到在 Chat 类中使用 MyApp\ChatManager;
  • 我添加了它,但我仍然得到 PHP 致命错误:第 37 行的 /var/www/soFitTest/chat/src/MyApp/ChatMananger.php 中找不到类 'MyApp\ChatManager'
【解决方案2】:

你的主要问题是这一行:

private $type = ChatManager::DEFAULT_CHAT_TYPE;

改成:

private $type = self::DEFAULT_CHAT_TYPE;

还有:

$chatManager = new \ChatMananger(1, 1);

应该是:

$chatManager = new MyApp\ChatManager(1, 1);

(如果我正确理解您的命名空间结构)


同样,您的 ChatManager 类应在其上方声明一个命名空间(以保持一致性)。完整的文件如下所示:

<?php

namespace MyApp;

require_once 'DBConnection.php';

class ChatManager
{
    const DEFAULT_CHAT_SIZE = 5;
    const DEFAULT_CHAT_TYPE = 1;

    private $dbConnection;
    private $chatId;
    private $senderId;
    private $receiverId;
    private $type = self::DEFAULT_CHAT_TYPE;

    public function __construct($senderId, $receiverId)
    {
        $this->dbConnection = DBConnection::getDBConnection();

        $this->senderId = $senderId;
        $this->receiverId = $receiverId;
    }

    public function getDBConnection()
    {
        return $this->dbConnection;
    }
}

【讨论】:

    猜你喜欢
    • 2012-10-23
    • 2015-08-27
    • 2022-11-10
    • 2020-02-10
    • 1970-01-01
    • 2015-09-23
    • 2016-08-15
    • 2014-10-19
    相关资源
    最近更新 更多