Я пытаюсь использовать один из моих классов в моем пространстве имен, но я получаю класс не найдена ошибка:Класс не найден, используя include_once в пространстве имен
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
Класс ChatMananger
находится в 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;
}
}
EDIT
Я получил вещи работать в __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 ? – Bang