Reputation: 2031
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
Here is the code:
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);
}
}
My file structure is:
The code that I posted in the top of the page is found in Chat.php
The class ChatMananger
is found in ChatMananger.php
The ChatMananger.php
file:
<?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
I got things working in the __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;
}
}
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
Upvotes: 1
Views: 2246
Reputation: 7865
Your main issue is this line:
private $type = ChatManager::DEFAULT_CHAT_TYPE;
Change it to:
private $type = self::DEFAULT_CHAT_TYPE;
Also:
$chatManager = new \ChatMananger(1, 1);
should probably be:
$chatManager = new MyApp\ChatManager(1, 1);
(if I understand your namespace structure properly)
Similarly, your ChatManager class should declare a namespace above it (for consistency). The complete file would look as follows:
<?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;
}
}
Upvotes: 1
Reputation: 71
Import ChatManager first (Using namespaces in PHP):
include_once __DIR__.'/ChatMananger.php';
use MyApp\ChatManager;
And later use it like this:
$chatManager = new ChatMananger(1, 1);
Upvotes: 4