php中註冊器模式類用法執行個體分析,註冊器執行個體分析_PHP教程

來源:互聯網
上載者:User

php中註冊器模式類用法執行個體分析,註冊器執行個體分析


本文執行個體講述了php中註冊器模式類用法。分享給大家供大家參考,具體如下:

註冊器讀寫類
Registry.class.php

<?php/**  * 註冊器讀寫類  */class Registry extends ArrayObject{  /**    * Registry執行個體   *   * @var object    */  private static $_instance = null;  /**   * 取得Registry執行個體   *    * @note 單件模式   *    * @return object   */  public static function getInstance()  {    if (self::$_instance === null) {      self::$_instance = new self();      echo "new register object!";    }    return self::$_instance;  }  /**   * 儲存一項內容到註冊表中   *    * @param string $name 索引   * @param mixed $value 資料   *    * @return void   */  public static function set($name, $value)  {    self::getInstance()->offsetSet($name, $value);  }  /**   * 取得註冊表中某項內容的值   *    * @param string $name 索引   *    * @return mixed   */  public static function get($name)  {    $instance = self::getInstance();    if (!$instance->offsetExists($name)) {      return null;    }    return $instance->offsetGet($name);  }  /**   * 檢查一個索引是否存在    *    * @param string $name 索引   *    * @return boolean   */  public static function isRegistered($name)  {    return self::getInstance()->offsetExists($name);  }  /**   * 刪除註冊表中的指定項   *    * @param string $name 索引   *    * @return void   */  public static function remove($name)  {    self::getInstance()->offsetUnset($name);  }}

需要註冊的類

test.class.php

<?phpclass Test{   function hello()   {    echo "hello world";    return;   }} ?>

測試 test.php

<?php//引入相關類require_once "Registry.class.php";require_once "test.class.php";//new a object$test=new Test();//$test->hello();//註冊對象Registry::set('testclass',$test);//取出對象$t = Registry::get('testclass');//調用對象方法$t->hello();?>

希望本文所述對大家php程式設計有所協助。

http://www.bkjia.com/PHPjc/1067836.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1067836.htmlTechArticlephp中註冊器模式類用法執行個體分析,註冊器執行個體分析 本文執行個體講述了php中註冊器模式類用法。分享給大家供大家參考,具體如下: 註冊器讀...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.