in my php project I want to use web socket and i face this problem that when i run server.php it says like "MyChatApp\User class is not found in chat.php
here is my code
chat.php
namespace MyChatApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface
{
protected $clients;
public $userObj,$data;
public function __construct() {
$this->clients = new \SplObjectStorage;
$this->userObj = new \MyChatApp\User;//the error is here
}
public function onOpen(ConnectionInterface $conn)
{
// Store the new connection to send messages to later
$this->clients->attach($conn);
var_dump($this->userObj->userData('11'));
echo "New connection! ({$conn->resourceId})\n";
}
user.php
<?php
namespace MyChatApp;
use PDO;
include_once 'login.php';
include_once 'session.php';
class User{
public $db,$id,$sessionID;
public function __construct(){
$db = new \MyChatApp\DB;
$this->db = $db->connect();
$this->id = $this->ID();
$this->sessionID = $this->getSessionID();
}
}
Personally I would use composer to autoload all the files. it does require them to follow a specific directory and filename structure, but most modern php apps work like this (PSR-4)
@habte Does the error occur because you are calling a certain file perhaps? If you call the other files directly, you would need to include the user.php in there as well. Normally we use index.php for all our requests, and build a router to call whatever code we need :)
@Sinnbeck yeah but when i loaded the user.php file the error will change to "Fatal error: Cannot declare class MyChatApp\User, because the name is already in use in C:\wamp64\www\insa\User.php on line 6"