找不到类,在名称空间中使用include_once [英] Class not found, using include_once in namespace

查看:259
本文介绍了找不到类,在名称空间中使用include_once的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的命名空间中使用我的一个类,但出现未找到的类错误:

I'm trying to use one of my classes in my namespace but I get a class not found error:

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

The code that I posted in the top of the page is found in Chat.php

ChatMananger.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中,我使用了:

Now my problem is that in the method onOpen I use:

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

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

For this code I get this error:

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

推荐答案

首先导入ChatManager(

Import ChatManager first (Using namespaces in PHP):

include_once __DIR__.'/ChatMananger.php';

use MyApp\ChatManager;

然后像这样使用它:

$chatManager = new ChatMananger(1, 1);

这篇关于找不到类,在名称空间中使用include_once的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆