1

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 ChatManangeris found in ChatMananger.php

enter image description here

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
1
  • 2
    Can you add the content of your ChatManager ? Commented Mar 3, 2015 at 11:13

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

8 Comments

I get this error: PHP Fatal error: Cannot redeclare class ChatMananger in /var/www/soFitTest/chat/src/MyApp/ChatMananger.php on line 6
Declare the namespace MyApp in your ChatManager class:
Adding the name space to the class, solved it, thanks!
I do not see use MyApp\ChatManager in Chat class;
I added it and I stll get PHP Fatal error: Class 'MyApp\ChatManager' not found in /var/www/soFitTest/chat/src/MyApp/ChatMananger.php on line 37
|
1

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.