2011-03-24 23 views
1

我已經玩過CodeIgniter,並擴展我的PHP知識,我試圖創建自己的框架。PHP get_instance /瞭解單例模式

我遇到的問題是我想要CodeIgniter get_instance()函數的一個等價物。但通過我所有的搜索,我只是無法理解它,我也不知道我是否在正確的背景下使用它。

我相信我在尋找的是單身模式,但我只是無法解決如何實施它,所以任何人都可以幫助我?

我希望能夠從內容函數中訪問框架的$ page變量。

[我要驚呼這是一個簡化版本,我的編碼通常比這更好..]

編輯之前:

<?php 

    class Framework { 
     // Variables 
     public $page; 

     function __construct() 
     { 

      // For simplicity's sake.. 
      $this->page->title = 'Page title'; 
      $this->page->content->h1 = 'This is a heading'; 
      $this->page->content->body = '<p>Lorem ipsum dolor sit amet..</p>'; 

      $this->output(); 

     } 

     function output() 
     { 

      function content($id) 
      { 

       // I want to get an instance of $this 
       // To read and edit variables 
       echo $this->page->content->$id; 
      } 

      ?> 
    <html> 
    <head> 
     <title><?php echo $this->page->title ?></title> 
    </head> 

    <body> 
     <h1><?php content('h1') ?></h1> 
     <?php content('body') ?> 
    </body> 
    </html> 
      <?php 
     } 

    } 

    new Framework; 

編輯後:

<?php 

class Framework { 
    // Variables 
    public $page; 

    public static function get_instance() 
    { 
     static $instance; 
     $class = __CLASS__; 
     if(! $instance instanceof $class) { 
      $instance = new $class; 
     } 
     return $instance; 
    } 

    function __construct() 
    { 

     // For simplicity's sake.. 
     $this->page->title = 'Page title'; 
     $this->page->content->h1 = 'This is a heading'; 
     $this->page->content->body = '<p>Lorem ipsum dolor sit amet..</p>'; 

     $this->output(); 

    } 

    function output() 
    { 

     function content($id) 
     { 
      $FW = Framework::get_instance(); 
      // I want to get an instance of $this 
      // To read and edit variables 
      echo $FW->page->content->$id; 
     } 

     ?> 
<html> 
<head> 
    <title><?php echo $this->page->title ?></title> 
</head> 

<body> 
    <h1><?php content('h1') ?></h1> 
    <?php content('body') ?> 
</body> 
</html> 
     <?php 
    } 

} 

new Framework; 

回答

5

得到實例方法通常這樣工作....

public static function getInstance() { 
    static $instance; 
    $class = __CLASS__; 

    if (! $instance instanceof $class) { 
    $instance = new $class; 
    } 

    return $instance; 
} 
  1. 設置一個靜態變量,該狀態通過static關鍵字保留。
  2. 設置一個指向類的變量(使維護更容易一些)。
  3. 檢查$instance是否是該類的實例化對象。
  4. 如果不是,那麼我們從這個類中實例化一個新的對象。
  5. 返回我們剛剛創建的新對象或現有對象。

所以調用Class::getInstance()會在第一次調用時返回一個新的對象,而subsequent calls will return the existing object

單身往往令人難以接受的幾個原因,和Wikipedia covers them rather well ...

This pattern makes unit testing far more difficult, as it introduces global state into an application.

It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialised, e.g., by locking.

Advocates of dependency injection would regard this as an anti-pattern, mainly due to its use of private and static methods.

Some have suggested ways to break down the singleton pattern using methods such as reflection in languages such as Java or PHP.

+0

感謝您的幫助,我已經編輯我最初的問題,包括在那裏,我認爲它應該去的單身,但我得到這個錯誤: '致命錯誤:無法在第32行的/home/beta2/public_html/sample.php中重新聲明內容()(以前在/home/beta2/public_html/sample.php:32中聲明) – StudioLE 2011-03-24 14:15:02

+0

我有點以爲我誤解了類和對象應該如何工作.. – StudioLE 2011-03-24 14:17:49