### 常驻内存没有上下文隔离 常驻内存下采用单例的控制器,不会因为每次请求都new一次控制器。 BaseController.php ```php class BaseController { protected $num = 0; } ``` Controller.php ```php class Controller extends BaseController { public function index(){ $this->num++; return "index:".$this->num; } public function test(){ $this->num++; return "test:".$this->num; } } ``` Server.php ```php include './BaseController.php'; include './Controller.php'; class Server { private $server; private $controller; public function __construct() { $this->controller = new Controller(); $this->server = new Swoole\Http\Server("127.0.0.1", 9501); $this->server->on('request', function ($request, $response){ $c = $request->get['c']??'index'; $res = call_user_func([$this->controller,$c]); $response->end($res); }); $this->server->start(); } } new Server(); ``` 分别请求一下: * http://127.0.0.1:9501/ * http://127.0.0.1:9501?c=test