2011-03-24 96 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