The essence of the task: in my project root is the server.php file. It is used to operate the socket.

I have described functions for sockets in it and I want, so that when some function works, I could do something in the project database.

For example, when a function is triggered that a new message has arrived, I want this new message to also be recorded in the database.

Server.php code

require 'vendor/autoload.php'; $io = new \PHPSocketIO\SocketIO(2020); $io->on('connection', function($socket){ $socket->addedUser = false; // when the client emits 'new message', this listens and executes $socket->on('new message', function ($message, $user_to, $user_from, $data, $img)use($socket){ // we tell the client to execute 'new message' $sms=new \frontend\models\Messages(); $sms->from_user=$user_from; $sms->to_user=$user_to; $sms->text=$message; $sms->data=$data; $sms->status=0; return $sms->save() ? $sms : null; } $socket->broadcast->emit('new message', array( 'message'=> $message, 'user_to'=> $user_to, 'user_from'=>$user_from, 'data'=>$data, 'img'=>$img )); }); }); \Workerman\Worker::runAll(); 

But this code does not work, says that it cannot find the Message class. How can I access it from this file?

    1 answer 1

    First, maybe new \ frontend \ models \ Message (), not Message s ().

    Second, check that namspace is correct. Go to the Message class and check its namespace.

    The third is more advice. Use use

     use \frontend\models\Messages 
    • in the class namespace is set as follows - namespace frontend \ models; in server.php tried and so - use \ frontend \ models \ Messages; and so - use frontend \ models \ Messages; - the result is the same - Fatal error: Uncaught Error: Class 'frontend \ models \ Messages' not found - Diefair