注冊樹模式是把物件掛到一個類的屬性陣列里,下次直接在這個陣列里面取,保持全域唯一,一般在專案入口初始化的時候有用到,在workerman中一開始的就是個注冊樹模式的運用,下面是對他的模擬
<?php class Worker{ protected static $_workers=array(); public function __construct() { $this->workerId=spl_object_hash($this); static::$_workers[$this->workerId]=$this; } public static function runAll(){ foreach (static::$_workers as $worker) { var_dump($worker); } } } new Worker(); new Worker(); Worker::runAll();
在Worker的建構式中,把當前new的物件掛到了Worker類的靜態變數屬性陣列里,在下次使用的時候直接在那個陣列里取

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/98216.html
標籤:PHP
上一篇:php redis擴展安裝步驟
