<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                `注冊樹模式`又稱`注冊模式`或`注冊器模式`。 注冊樹模式通過將對象實例注冊到一棵全局的對象樹上,需要的時候從對象樹上采摘的模式設計方法 注冊上去后可以無限采摘 創建好這個對象如何直接獲得這個對象呢就用注冊模式 ``` class Registry{ static private $_store=array(); /** * 將對象注冊到$_store上 * @param object $obj 類的實例化對象 * @param string|null $name 該對象的實際類名 */ static public function set($obj,$name=null){ if (!is_object($obj)) { throw new Exception("參數不是期望的類對象", 1); } $name=(!is_null($name))?:get_class($obj); $name=strtolower($name); $return=null; if (isset(self::$_store[$name])) { $return = self::$_store[$name]; } self::$_store[$name]=$obj; return $return; } static public function get($name){ $name=strtolower($name); if (!self::isContains($name)) { throw new Exception("該類的實例對象不存在", 1); } return self::$_store[$name]; } static public function remove($name){ $name=strtolower($name); if (self::isContains($name)) { unset(self::$_store[$name]); } } /** * 檢查給定的類的類對象是否掛載到注冊樹($_store)上 * @param [type] $name [description] * @return boolean [description] */ static public function isContains($name){ $name=strtolower($name); if (!isset(self::$_store[$name])) { return false; } return true; } } class Fruit{} $std=new stdClass(); Registry::set($std); $fruit=new Fruit(); Registry::set($fruit); $std=Registry::get("stdClass"); $fruit=Registry::get("Fruit"); var_dump($std); var_dump($fruit); ``` 例子2:與工廠模式結合 ``` <?php 前面的工廠加單例模式用到的話還是需要調用工廠或者。。定義好后如何取到這個類呢一般就要用到注冊模式 用來將一些對象注冊到全局樹上面 就可以在任何地方訪問 還可以給對象取別名; 工廠模式和注冊模式配合使用,放到程序初始化里面 Register.php: namespace IMooc; class Register{ protected static $objects; //第一個參數是第二個參數(對象)的映射的名(將對象放到樹上) static function set($alias,$object){ self::$objects[$alias] = $object; } //unset是php的關鍵字所以前面加了個_ function _unset(){ return self::$objects[$alias]; } static function get($objectname){ if (!isset(self::$objects[objectname])) { throw new Exception("對象不存在", 1); } return self::$objects[objectname]; } } namespace IMooc; class Factory{ static function createDatabase(){ //$db = new Database(); $db =Database::connectDB(); Register::set('db1',$db);//調用注冊類的方法將db對象放進注冊樹中對象的別名叫db1 return $db; } } 其他任何文件: 獲取對象:$db = \IMooc\Register::get('db1'); database.php: class Database { public static $mark = null; // 定義標識符(通過$mark值得改變情況,來判定Model類是否被實例化) // __construct()只執行一次 private final function __construct(){ // 鏈接數據庫 mysql_connect('localhost','root',''); echo '數據庫鏈接成功<br>'; } // 申明靜態方法,由類名直接調用此方法,來實例化Model類 // 在靜態方法中,絕不可以使用$this($this是對象產生之后才會有的) public static function connectDB() { // 實例化Model類 if (self::$mark == null) { // self:代表自身(self=Model) self::$mark = new self(); } return self::$mark; } public function select(){ echo '查詢數據'; } public function insert(){ } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看