Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

luk's avatar
Level 1

Class 'MyChatApp\User' not found

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

}
0 likes
17 replies
Sinnbeck's avatar

I would use the entry point (index.php?) to include all the files, instead of trying to do it here and there. Or use composer to do it for you :)

luk's avatar
Level 1

@Sinnbeck I am using composer and I have composer.json file but I dont get your point ?

Sinnbeck's avatar

@habte Well I dont have access to all of your files, but I assume it all loads from an index.php or similar?

In that file, you can include the files that is needed for the project

<?php
include_once 'login.php';
include_once 'session.php';
include_once 'user.php';
//etc

//the code that starts your proiect
Sinnbeck's avatar

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)

https://www.php-fig.org/psr/psr-4/

luk's avatar
Level 1

@Sinnbeck I can post all the file you want but I posted only the two ones because the error is there from chat.php in line 13

Sinnbeck's avatar

@habte Any chance it is on github so we can inspect the code there? Its a bit easier that getting a ton of raw php code here :)

1 like
Sinnbeck's avatar

@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 :)

1 like
luk's avatar
Level 1

@Sinnbeck I use namespace and after that i want to use user class which is found in user.php and i call like it

$this->userObj = new \MyChatApp\User;

in chat.php and the error is indicated at line 13 which is the above code

luk's avatar
Level 1

@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"

luk's avatar
Level 1

@Sinnbeck Do you have any suggestion that could be the reason for the error I am so tired of this problem please

knubbe's avatar

Can you try with just User because that class share same namespace

1 like
luk's avatar
Level 1

any one who can help me to fix this problem please please

Please or to participate in this conversation.