We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

In a file use namespace, which cannot include the second class?

when In my library , I have a file named socket, the file are follow:

  > <?php
  > namespace Library;
  > 
  > use Phalcon\Mvc\User\Component;
  > 
  > // Run from command prompt > php chat.php
  > use Devristo\Phpws\Framing\WebSocketFrame;
  > use Devristo\Phpws\Framing\WebSocketOpcode;
  > use Devristo\Phpws\Messaging\WebSocketMessageInterface;
  > use Devristo\Phpws\Protocol\WebSocketTransportInterface;
  > use Devristo\Phpws\Server\IWebSocketServerObserver;
  > use Devristo\Phpws\Server\UriHandler\WebSocketUriHandler;
  > use Devristo\Phpws\Server\WebSocketServer;
  > 
  > class execClass extends Component{
  > 
  > public function init(){
  > $loop = \React\EventLoop\Factory::create();
  > 
  > // Create a logger which writes everything to the STDOUT
  > $logger = new \Zend\Log\Logger();
  > $writer = new \Zend\Log\Writer\Stream("php://output");
  > $logger->addWriter($writer);
  > 
  > // Create a WebSocket server
  > $server = new WebSocketServer("tcp://0.0.0.0:12345", $loop, $logger);
  > 
  > // Create a router which transfers all /chat connections to the ChatHandler class
  > $router = new \Devristo\Phpws\Server\UriHandler\ClientRouter($server, $logger);
  > // route /chat url
  > $router->addRoute('#^/chat$#i', new ChatHandler($logger));
  > // route unmatched urls durring this demo to avoid errors
  > $router->addRoute('#^(.*)$#i', new ChatHandlerForUnroutedUrls($logger));
  > 
  > // Bind the server
  > $server->bind();
  > 
  > // Start the event loop
  > $loop->run();
  > }
  > }
  > 
  > /**
  > * This ChatHandler handler below will respond to all messages sent to /chat (e.g. ws://localhost:12345/chat)
  > */
  > class ChatHandler extends WebSocketUriHandler {
  > 
  > /**
  > * Notify everyone when a user has joined the chat
  > *
  > * @param WebSocketTransportInterface $user
  > */
  > public function onConnect(WebSocketTransportInterface $user){
  > foreach($this->getConnections() as $client){
  > $client->sendString("User {$user->getId()} joined the chat: ");
  > }
  > }
  > 
  > /**
  > * Broadcast messages sent by a user to everyone in the room
  > *
  > * @param WebSocketTransportInterface $user
  > * @param WebSocketMessageInterface $msg
  > */
  > public function onMessage(WebSocketTransportInterface $user, WebSocketMessageInterface $msg) {
  > $this->logger->notice("Broadcasting " . strlen($msg->getData()) . " bytes");
  > 
  > foreach($this->getConnections() as $client){
  > $client->sendString("User {$user->getId()} said: ".$msg->getData());
  > }
  > }
  > }
  > 
  > class ChatHandlerForUnroutedUrls extends WebSocketUriHandler {
  > /**
  > * This class deals with users who are not routed
  > */
  > public function onConnect(WebSocketTransportInterface $user){
  > //do nothing
  > $this->logger->notice("User {$user->getId()} did not join any room");
  > }
  > public function onMessage(WebSocketTransportInterface $user, WebSocketMessageInterface $msg) {
  > //do nothing
  > $this->logger->notice("User {$user->getId()} is not in a room but tried to say: {$msg->getData()}");
  > }
  > }
  > 

tip:Fatal error: Class 'Library\execClass' not found in D:\Desktop\backk\app\modules\Compute\controllers\ShowController.php on line 18

help me



85.5k
Accepted
answer

you can't do that, and also you should not.

Phalcon uses the Class name and search for a (phisical) file named like the class itself.

Also I dont think it's a good pratice to put 2 classes in the same file.