PHP get_instance / Understanding the Singleton pattern
I've played around with CodeIgniter, and to expand my PHP knowledge I'm attempting to create my own framework.
The issue I have is I want an equivalent of the CodeIgniter get_instance() function. But through all my searching I just can't understand it, nor do I know whether I'm even using it in the correct context..
I believe what I'm looking for is the singleton pattern, but I just can't work out how to implement it, so can anyone help me out?
I want to be able to access the the $page variable for the framework from within the content function.
[I wish to exclaim this is a simplified version and my coding is normally better than this..]
Before Edit:
<?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;
After Edit:
<?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;
A get instance method usually works like this....
public static function getInstance() {
static $instance;
$class = __CLASS__;
if ( ! $instance instanceof $class) {
$instance = new $class;
}
return $instance;
}
- Set up a static variable thats state is preserved with the
static
keyword. - Set up a variable that points to the class (makes maintenance a bit easier).
- Check to see if
$instance
is an instantiated object from this class. - If it is not, then we instantiate a new object from this class.
- Return either the new object we just made or the existing object.
So calling Class::getInstance()
will return a new object when first called, and subsequent calls will return the existing object.
Singletons are often frowned upon for a few reasons, and 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.
Found a pretty good article on the singleton pattern with PHP. The guy does a pretty good job on explaining it: http://phpadvocate.com/blog/?p=211
精彩评论