MyClass.php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
class MyClass{
public function __construct(){}
public function onWorkerStart(Worker $worker){}
public function onConnect(TcpConnection $connection){}
public function onMessage(TcpConnection $connection, $message) {}
public function onClose(TcpConnection $connection){}
public function onWorkerStop(TcpConnection $connection){}
}
启动脚本 start.php
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
// 载入MyClass
require_once __DIR__.'/MyClass.php';
$my_object = new MyClass();
// 调用类的方法
### $worker->on=$my_object;//诸如这样
避免只能这样
$worker->onWorkerStart = array($my_object, 'onWorkerStart');
$worker->onConnect = array($my_object, 'onConnect');
$worker->onMessage = array($my_object, 'onMessage');
$worker->onClose = array($my_object, 'onClose');
$worker->onWorkerStop = array($my_object, 'onWorkerStop');