Strategy:
<?php
interface FlyBehavior {
public function fly();
}
interface QuackBehavior {
public function quack();
}
class FlyWithWings implements FlyBehavior {
public function fly() {
echo "I'm flying!!\n";
}
}
class FlyNoWay implements FlyBehavior {
public function fly() {
echo "I can't fly!\n";
}
}
class FlyRocketPowered implements FlyBehavior {
public function fly() {
echo "I'm flying with a rocket!\n";
}
}
class Qquack implements QuackBehavior {
public function quack() {
echo "Quack\n";
}
}
class Squeak implements QuackBehavior {
public function quack() {
echo "Squeak\n";
}
}
class MuteQuack implements QuackBehavior {
public function quack() {
echo "<< Silence >>\n";
}
}
abstract class Duck {
protected $quack_obj;
protected $fly_obj;
public abstract function display();
public function performQuack() {
$this->quack_obj->quack();
}
public function performFly() {
$this->fly_obj->fly();
}
public function swim() {
echo "All ducks float, even decoys!\n";
}
public function setFlyBehavior(FlyBehavior $fb) {
$this->fly_obj = $fb;
}
public function setQuackBehavior(QuackBehavior $qb) {
$this->quack_obj = $qb;
}
}
class ModelDuck extends Duck {
public function __construct() {
$this->fly_obj = new FlyNoWay();
$this->quack_obj = new MuteQuack();
}
public function display() {
echo "I'm a model duck!\n";
}
}
$model = new ModelDuck();
$model->performFly();
$model->performQuack();
$model->setFlyBehavior(new FlyRocketPowered());
$model->setQuackBehavior(new Squeak());
$model->performFly();
$model->performQuack();
?>
Patterns
Patterns are ways to describe best practices and good designs. They show a flexible solution to common programming problems.
Factory
The Factory pattern allows for the instantiation of objects at runtime. It is called a Factory Pattern since it is responsible for "manufacturing" an object. A Parameterized Factory receives the name of the class to instantiate as argument.
Example #1 Parameterized Factory Method
<?php
class Example
{
// The parameterized factory method
public static function factory($type)
{
if (include_once 'Drivers/' . $type . '.php') {
$classname = 'Driver_' . $type;
return new $classname;
} else {
throw new Exception ('Driver not found');
}
}
}
?>
Defining this method in a class allows drivers to be loaded on the fly. If the Example class was a database abstraction class, loading a MySQL and SQLite driver could be done as follows:
<?php
// Load a MySQL Driver
$mysql = Example::factory('MySQL');
// Load a SQLite Driver
$sqlite = Example::factory('SQLite');
?>
Singleton
The Singleton pattern applies to situations in which there needs to be a single instance of a class. The most common example of this is a database connection. Implementing this pattern allows a programmer to make this single instance easily accessible by many other objects.
Example #2 Singleton Function
<?php
class Example
{
// Hold an instance of the class
private static $instance;
// A private constructor; prevents direct creation of object
private function __construct()
{
echo 'I am constructed';
}
// The singleton method
public static function singleton()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
// Example method
public function bark()
{
echo 'Woof!';
}
// Prevent users to clone the instance
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
?>
This allows a single instance of the Example class to be retrieved.
<?php
// This would fail because the constructor is private
$test = new Example;
// This will always retrieve a single instance of the class
$test = Example::singleton();
$test->bark();
// This will issue an E_USER_ERROR.
$test_clone = clone $test;
?>
Patterns
14-Aug-2008 05:44
12-Aug-2008 03:48
Apologies to tedmasterweb, since I was incorrect in my note just prior. His sanitizing method would work, since it is based on known values from a switch statement. I had read it too quickly as the more common method of simply appending ".php" to variable values used in include directives in order to ensure that included files are PHP files, like so in a common __autoload variety often seen in the wild:
<?php
function __autoload($class) {
require_once("path/to/classes/$class.php");
}
?>
This was the sort of unsafe usage of the include directives I was commenting on.
12-Aug-2008 03:41
Tedmasterweb's point is a good one, though sanitizing inputs is often even more subtle than you would expect. For instance, his sanitizing solution would fail if the supplied driver name were "../../../../../../../../etc/passwd\0" (which, as you'll note, is null-byte-terminated). The underlying C-functions that run most modern POSIX-compliant and POSIX-based operating system filesystem operations will treat the ASCII null byte as a string termination character, and so will ignore the ".php" he appended to the path. Thus, this attack would still succeed. This attack is described in further detail here (the link is too long for the word wrapping, but it will still work):
http://us3.php.net/manual/en/security.filesystem.php
#security.filesystem.nullbytes
In my own __autoload functions, I tend to provide a validating regex for class or interface names to only allow characters I know are safe (though this code might have to be adjusted once namespace support is fully functional in PHP). For anyone who prefers to attempt to remove unsafe characters instead, I suggest the following list at a minimum:
period (for the ".." pseudo-directory), slash and backslash (for directory separators; both work on Windows systems), the explicit DIRECTORY_SEPARATOR constant, and the ASCII null byte (which can be typed in code as "\0").
Usage of the PHP functions escshellcmd or escshellarg might also be advisable, depending on how your code is written (particularly depending on whether it is executing a shell command or including a file).
26-Jun-2008 10:21
Regarding Example #1:
Be sure to "sanitize" your variables before using them for including files.
Example #1 assumes the variable $type contains sanitized data (and NOT something threatening like '../../../../../etc/passwd').
One approach might be as follows:
<?php
switch ( $type ) {
case "MySQL":
$type = 'mysql';
break;
case "SQLite":
$type = 'sqlite';
break;
default:
throw new Exception ('Driver not found');
}
if (include_once 'Drivers/' . $type . '.php') {
$classname = 'Driver_' . $type;
return new $classname;
} else {
throw new Exception ('Driver not found');
}
?>
Naturally, the examples are brief and intended to express the issue at hand, not input sanitizing methods, but someone might find this note helpful.
HTH
03-Jun-2008 09:04
Db layer design algorithm:
"Separation of database layer by database functionality"
First we draw this line:
Application
-------------
DB
Here comes the new thing:
The db layer we separate it into two areas like this:
Db layer
|
select | insert, update delete
|
in the left side we have the select part.
The difference between the left and the right sides is that select side has no connection with the db structure - you can take data querying all table, while the right side is table oriented.
Select side:
Here all queries are in one file called "getters". Like getterUsers.
Inside it will be the body of the query but the fields that are to be selected will be build dynamically.
When calling this "getter" you will provide an array of fields to be selected or no fields, situation when it will take all fields.
To keep those db field names as low as possible in this architecture - so the above code dont need to know about the name of the fields in db, you will make just above this "getter", a "filter" file.
This filter will have a switch with a key and inside will call the getter with fields from db.
Ex:
<?php
switch(key)
case 'Label':
$fields = array("label", "firstName");
$obj->getUser($fields)
break;
?>
Above this filter you will have a "caller" that will call the "filter" with the key:
Ex:
<?php
function callerUserLabel()
{
->filterUserLabel('Label');
}
?>
So in the higher code you just make
->callerUserLabel();
and this will return all what you need.
If you want this query to get out of the db and another field called "email", you just create another case in the switch and a "caller" to call that case.
Dynamic and extensible.
----------------
Now on the insert update side.
Here we only think in db tables.
First we do some "checkers" for all fields in db.
checkerTableName.php
This will contain an array with all the fields and the checks that must be done on that field and perform the checks only on the provided fields.
Then we do the "updaters". Those are build dinamically like the "getters" - if fields provided, only those updated. If all fields provided, can be used as an "inserter".
When performing an insert, all checks on that table will be done.
When performing an update on only one field - like updating a label, only that label will be checked to be in conformity with requirements like a length, special chars inside.
Dependency checks are made here also.
When having in one application action a multiple insert or update (when inserting an operator also add a webUser lets say) you do above those "inserter" and "updater" files a small "manager" that will do:
->checkOperatorFields();
->checkWebUserFields();
->insertOperator();
->insertWebUser();
If no error is thrown, all things will go nice and quiet.
Now also the "getters" will have their fields checked when needed using those checkers. I am talking about the values that are given as parameters not about the ones that are returned from db.
If you are working with domain objects or business logic objects (whatever you call them) above the db layer you will have to make below that line we first draw a "business logic asambler" object that will make from your data what you want.
If this dont exist, at least an intermediary layer that will map the names from db with the ones used in application must be done in both directions - from application to db and from db to application.
-------------------------------------
Advantages of this architecture:
- Usefull in complex application especheally where the selects are made ussually from many tables so you dont have to load lots and lots of files and to get lost in the logic for one simple select.
- NO DUPLICATE CODE
- Simple and intuitive.
- Flexible and expendable.
-----------
"A place where this idea is exposed completely or to debate on it:"
http://www.kurzweilai.net/mindx/show_thread.php?rootID=122913
01-Jun-2008 08:49
A tricky way to implement Factory or Singleton is this one:
<?php
class A {
// your amazing stuff
}
// implicit factory
function A( /* one or more args */ ){
return new A( /* one or more args */ );
}
$a = A(1,2,3);
$b = A(2,3);
// or implicit Singleton
function A( /* one or more args */ ){
static $instance;
return isset($instance) ? $instance : ($instance = new A( /* one or more args */ ));
}
A(1,2,3) === A(2,3); // true
?>
More details in WebReflection, byez
21-May-2008 06:43
The Singleton pattern shown in the above example can still be broken by object serialization. e.g.
<?
class SingleFoo{
private static $instance;
public $identity = "in the end, there can be only one foo\n";
private function __construct() {}
public static function getInstance(){
if(!self::$instance){
self::$instance = new SingleFoo();
}
return self::$instance;
}
public function setIdentity($identity){
$this->identity = $identity;
}
public function getIdentity(){
return $this->identity;
}
}
$foo = unserialize( serialize(SingleFoo::getInstance()) );
$foo->setIdentity("new foo on the block\n");
print(SingleFoo::getInstance()->getIdentity());
print($foo->getIdentity());
// result:
// in the end, there can be only one foo
// new foo on the block
?>
A possible solution to this problem involves using the magic method __wakeup() in the Singleton class to enforce the Singleton pattern during unserialization:
<?
class Singleton{
// ....
public function __wakeup(){
if(!self::$instance){
self::$instance = $this;
}else{
trigger_error("Unserializing this instance while another exists voilates the Singleton pattern",
E_USER_ERROR);
}
}
// ....
}
?>
The unserialization would be permitted if it does not result in additional instances of the Singleton, and instances restored in this way would still be accessible via the Singleton static factory method.
Kris Dover
13-May-2008 10:00
Re: Chris
Question re: // Mangle the arguments into an array
Why not just use func_get_args() ? Am I missing something?
I mean, (no offense) but generally speaking I'm not a fan of what you put together.
I can see the benefit of abstracting the plumbing needed for a singleton in a common base class.
So I understand your thinking.
But I disagree with it. IMO creating a class hierarchy is about properly modeling a system (in the case of an event model) or an entity (in the case of a data model).
So instead of using inheritance to better solve a problem domain, I'm using it to handle an implementation detail.
IMO, creating an ISingleton interface to enforce continuity, and a code template of an empty class w/ all the required singleton programming would be much better.
05-May-2008 11:20
If you want a singleton and you don't mind on throwing a stop error/exception when a 2nd instace is created instead of returning the already created object, then this code works with subclassing and without getInstace alike functions (just with __construct)
<?php
class SINGLETON
{
// the array for checking the created instances
protected static $singletons = array ();
// make it final so extended classes cannot override it
final public function __construct ()
{
$classname = get_class ( $this );
if ( ! isset ( self::$singletons[$classname] ) )
// instead of $this it could be any value since we are checking for isset, but a reference to the object may be useful in a possible improvement, somewhat
self::$singletons[$classname] = $this;
else
// your favorite stop error here
trow new Exception ();
}
// final as well, no overriding, plus protected so it cannot be called
final protected function __clone () {}
}
class X extends SINGLETON
{
//...
}
class Y extends X
{
//...
}
$x = new X ();
$y = new Y ();
$a = new X (); // execution stops, as an instance of X is created already
$b = new Y (); // execution stops, as an instance of Y is created already, it DOES NOT give problems with X, despite extending X
?>
so, if it is ok to stops the execution, this method is way simpler
it must stops the execution because no matter what, __construct will return a reference to the new object, always, so if the script goes on further you will have effectively 2 instances (as a matter of fact, the code above only checks the existence of another object from the same class, it does not prohibit the creation of a new one)
hth
18-Apr-2008 07:07
I cannot belive I was so stupid with my first note on this...
As any Singleton class forces a design pattern on to the user, and is really just a helper class for that design pattern, there is no reason not to force them to mangle any arguments into an array, which allows me to get rid of the use of the ReflectionClass and allows protected constructors.
If someone can delete the first note to hide my shame that would be great, and this bit of this one upto the ----- marker below :D
To save people the trouble of finding my previous note:
-----
This is a Singleton helper class inspired by Mattlock's Singleton class with interface.
It has the advantages of:
1) It uses only a single abstract class instead of an interface and class definition.
2) It allows for multiple instances of classes where you only wish to have one of any instance initialised with certain arguments such as connections to different databases, they may use the same class, but there will be one instance for each database.
This method should allow multiple child classes to extend the Singleton class, and only requires a small about of argument mangling to make it work.
<?php
abstract class Singleton
{
static abstract public function getInstance();
static final protected function getClassInstance($klass, $args=NULL)
{
// Initialize array for $instances
if(self::$instances === NULL)
self::$instances = array();
// Initialize array for instances of $klass
if(!array_key_exists($klass, self::$instances))
self::$instances[$klass] = array();
// Create key from args
$key = serialize($args);
// Instantiate $klass
if(!array_key_exists($key, self::$instances[$klass]))
{
self::$instances[$klass][$key] = new $klass($args);
}
// Return instance of $klass
return self::$instances[$klass][$key];
}
static private
// Storage for all instances of Singleton classes
$instances;
}
final class MySingleton extends Singleton
{
static public function getInstance()
{
return Singleton::getClassInstance(__CLASS__);
}
// Construct should be protected.
protected function __construct() {}
}
final class NamedSingleton extends Singleton
{
public
$name;
// Default arguments here NOT constuctor to prevent dupilicate instances.
static public function getInstance($name = 'Default')
{
// Mangle the arguments into an array
$args = array('name' => $name);
return Singleton::getClassInstance(__CLASS__, $args);
}
// Constructor must accept a single array containing all the arguments, if it wants to get any.
protected function __construct($args)
{
$this->name = $args['name'];
}
}
echo '<pre>';
$a = MySingleton::getInstance();
$b = MySingleton::getInstance();
var_dump($a);
var_dump($b);
$c = NamedSingleton::getInstance();
$d = NamedSingleton::getInstance('Default');
$e = NamedSingleton::getInstance('Default');
var_dump($c);
var_dump($d);
var_dump($e);
echo '</pre>';
?>
18-Apr-2008 05:11
This is an extension to Mattlock's Singleton class and interface.
It has a number of advantages:
1) It uses a single abstract class instead of an interface and class definition, which to me is cleaner.
2) It allows for multiple instances of one class where you only wish to have one of any instance initialised with certain arguments. For example a File class representing a file on the system, there will be multiple files, but you will only want one instance of the File class for each individual file.
but a number of disadvantages:
1) The use of ReflectionClass is far from ideal, but I cannot find a better way to initialise an "unknown" class with arguments to the constructor, without requiring another 'utility' method in the child class.
2) The __construct method of child classes MUST be declared 'public' so that the ReflectionClass can instantiate it.
if there is a good way to instantiate a class in a function that has its name as a string and an array holding its arguments, without using eval etc, please tell me :D
<?php
abstract class Singleton
{
static abstract public function getInstance();
static final protected function getClassInstance($klass)
{
// Initialize array for $instances
if(self::$instances === NULL)
self::$instances = array();
// Get arguments and remove $klass
$args = func_get_args();
array_shift($args);
// Create key from args
$key = serialize($args);
// Initialize array for instances of $klass
if(!array_key_exists($klass, self::$instances))
self::$instances[$klass] = array();
// Instantiate $klass using reflection and store under $key
if(!array_key_exists($key, self::$instances[$klass]))
{
if(count($args) > 0)
{
$reflect = new ReflectionClass($klass);
self::$instances[$klass][$key] = $reflect->newInstanceArgs($args);
}
else
{
// ReflectionClass cannot pass arguments to classes with no constructor.
self::$instances[$klass][$key] = new $klass();
}
}
// Return instance of $klass
return self::$instances[$klass][$key];
}
static private
// Storage for all instances of Singleton classes
$instances;
}
class MySingleton extends Singleton
{
static public function getInstance()
{
return Singleton::getClassInstance(__CLASS__);
}
}
class ArgSingleton extends Singleton
{
public
$arg;
// Default arguments here NOT constuctor to prevent dupilicate instances.
static public function getInstance($arg = 'Default')
{
return Singleton::getClassInstance(__CLASS__, $arg);
}
public function __construct($arg)
{
$this->arg = $arg;
}
}
echo '<pre>';
$a = MySingleton::getInstance();
$b = MySingleton::getInstance();
var_dump($a);
var_dump($b);
$c = ArgSingleton::getInstance();
$d = ArgSingleton::getInstance('Default');
$e = ArgSingleton::getInstance('Default');
var_dump($c);
var_dump($d);
var_dump($e);
echo '</pre>';
?>
07-Apr-2008 07:27
I have read much of the debate between the singleton pattern and global variables. I think there are two advantage the singleton has over a global variable.
1.) The singleton can provide protection of the data it holds. The singleton class can define methods that will proper validate and store data.
2.) A singleton it more maintainable in two ways. Changing the underlying data structure, say a string to an array, will take much less effort using a singleton than using a global variable where you would have to find every time the global variable is modified (which can be mitigated with regular expressions). Second, the global variable can be changed and modified at any point in your code base which can cause problems with debugging large code bases, or code that relies on different libraries.
A practical example would be a way to log messages in an application.
With a global variable you can do:
<?PHP
$g_log = '';
// or
$g_log = array();
// But at any point $log can be wiped out or changed. i.e.
$g_log = 42;
// Where as a singleton can't in inadvertently lose data.
class Log {
public static function instance() {
static $_instance = null;
if (is_null($_instance)) {
$_instance = new Log();
}
return $_instance;
}
public function logMsg($s) {
// Validate
// Now add
}
}
$s_log = Log::instance();
// So if happens somewhere, unexpectedly
$s_log = 42;
// You will still have your log in the Log class and
$s_log->log("Hello World!");
/* Where as if $g_log was originally a string and you
wanted to change it to a global you would have to change */
$g_log .= 'Hello World!';
// to
$g_log[] 'Hello World!';
?>
If you change the underlying way in which the Log class stores the messages the above statement will not have to be changed.
If you want the speed or don't care about the security then use a singleton. If you want the security or maintainability then use a singleton.
Finally, the best thing you can do is use a consistent coding style throughout your code no matter what approach you take.
21-Feb-2008 06:16
This allows for singleton functionality via class extension, and does not use any shady functions like eval.
<?php
interface ISingleton {
public static function GetInstance();
}
class Singleton implements ISingleton {
public static function GetInstance($class='Singleton') {
static $instances = array();
if (!is_string($class)) {
return null;
}
if ($instances[$class] === null) {
$instances[$class] = new $class;
}
return $instances[$class];
}
}
class Mars extends Singleton implements ISingleton {
public static function GetInstance() {
return Singleton::GetInstance(get_class());
}
}
class Neptune extends Singleton implements ISingleton {
public static function GetInstance() {
return Singleton::GetInstance(get_class());
}
}
$x = Mars::GetInstance();
echo '<pre>'.print_r($x,true).'</pre>';
$x = Neptune::GetInstance();
echo '<pre>'.print_r($x,true).'</pre>';
?>
17-Jan-2008 07:55
For anyone who reads: tims57 at yahoo dot com
Please let me show everyone a example of how a global variable is nothing like a singleton pattern. Take into considering the following. A global variable, cares not the state of its contents, it is only a container. If it contains a instance, a boolean, or a integer, it is all the same to it as PHP is dynamically typed it would be very easy for a global variable to be overridden by some dynamically loaded module in a complicated architecture, which could in turn cause a instance that maintains account or billing data to destruct before values are written to it.
The singleton is a means of protecting the instance to a single instantiation.
A global variable offers no such protection, it is only a container for a value.
<?php
$unreliableInstanceNotProtectedAtAll = new StdClass;
class Singleton
{
static private $_instance;
private $_foo = 'foo';
private function __construct() {}
static public function getInstance() {
return isset(self::$_instance) ?
self::$_instance : self::$_instance = new self();
}
public function updateFoo($foo) {
$this->_foo = $foo;
}
public function getFoo() {
return $this->_foo;
}
}
/** Singleton offers protection from being instantiated multiple times */
// file_2481_r4.php, the early foo module not used except under certain circumstances
$foo = Singleton::getInstance();
var_dump($foo->getFoo()); // foo
$foo->updateFoo('bar');
var_dump($foo->getFoo()); // bar
// file_1444114_r1.php (author has no idea that file_2481_r4.php was loaded by
// the "foo" module", doesn't matter because the class will be instantiated only once
$foo = Singleton::getInstance();
var_dump($foo->getFoo()); // bar
/** Global variable just contains a instance, has no care for what it contains */
// file_2481_r4.php, the early foo module not used except under certain circumstances
$unreliableInstanceNotProtectedAtAll->foo = 'foo';
// file_1444114_r1.php (author has no idea that file_2481_r4.php was loaded by
// the "foo" module", doesn't matter because the class will be instantiated only once
$unreliableInstanceNotProtectedAtAll = new StdClass;
$unreliableInstanceNotProtectedAtAll->foo = 'bar';
?>
09-Jan-2008 11:01
Simplest PHP5 singleton implementation ever:
<?php
class Some_Class
{
static private $_i;
private function __construct()
{
}
static public function singleton() {
return isset(self::$_i) ? self::$_i : self::$_i = new self();
}
}
?>
31-Dec-2007 10:57
For anyone who reads: me at chrisstockton dot org
The note being refered to by that post is actually correct. A singleton provides essentially the same functionality as a global variable refering to an object combined with code to create the object at first use or application initialization. Using them is just a much more involved and difficult to follow technique involving class instantiation. Although perhaps appropriate in some circumstances, for many uses the global variable approach makes more sense.
The note being refered to is also correct regarding design patterns. Using design patterns and using interfaces should not be confused, they are nothing the same. Authors of 'pay-for software' (those 'large scale enterprise applications') have to get our laughs some way or another!
One final point neither note writer seems to get. PHP 5 accommodates many architectures, and is quite valuable for building scalable applications, even though it is still (thank God) free. PHP is now one of the 'big guys', and it needs these object oriented capabilities, even though they might be confusing to some.
25-Sep-2007 11:23
For anyone who reads: anonymous at world4ch dot org
Please do not confuse the singleton for a global variable, they are nothing the same. The global keyword is a way to extract a variable from the global scope into your local scope. A singleton is a way to control class instantiation, independent of scope to a single instance.
In addition note the importance in design patterns, the ability to interchange interfaces and abstraction to allow easy adaption to a variety of needs and specifications through a single means of access is invaluable to many large scale enterprise applications. Specially pay-for software that has many system architectures to accommodate. However, the comment makes a good point to implement as necessary, the many principles don't apply to every problem domain. Do not conform your problem to a pattern, implement a pattern to your problem.
16-Jul-2007 11:37
A singleton is the "enterprise professional scalable business solution" version of a global variable. It'd be a good idea if this were the IOCCC, but since it isn't, it's better to simply say:
global $lol;
It's also faster and more maintainable, and has the same advantages and disadvantages.
Also, I'll advise against all "enterprise object-oriented industry-standard design patterns". Some of these patterns are correct, but their existence is not. Programming is not about copypasta. It's about thinking, abstracting and combinating. If you can't think of these patterns for yourself when you need them — and in the case of needing them, manage to abstract them so that your code doesn't become a copypasta fest, then you shouldn't be programming in the first place.
Furthermore, the existence of software patterns is a proof of the object-oriented model shortcomings. You shouldn't ever need to do any copypasta in your code. If something is done two or more times, you abstract it. And the language should support a way for you to. There's no reason for a "design pattern" to exist more than once, and since they are trivial (they better be trivial to you, if you are programming), you don't need to study them.
The best advice I can give you is that instead of reading "design patterns", you read Structure and Interpretation of Computer Programs (SICP), a book freely available from MIT where you will learn what you need to program properly in any programming language.
19-Jun-2007 01:09
This's a example of Composite Pattern:
<?php
abstract class Graphic{
abstract public function draw();
}
class Triangle extends Graphic{
private $name = '';
public function __construct($name = 'unknown'){
$this->name = $name;
}
public function draw(){
echo '-I\'m a triangle '.$this->name.'.<br>';
}
}
class Container extends Graphic{
private $name = '';
private $container = array();
public function __construct($name = 'unknown'){
$this->name = $name;
}
public function draw(){
echo 'I\'m a container '.$this->name.'.<br>';
foreach($this->container as $graphic)
$graphic->draw();
}
public function add(Graphic $graphic){
$this->container[] = $graphic;
}
public function del(Graphic $graphic){
unset($this->container[$graphic]);
}
}
$tri1 = new Triangle('1');
$tri2 = new Triangle('2');
$tri3 = new Triangle('3');
$container1 = new Container('1');
$container2 = new Container('2');
$container3 = new Container('3');
$container1->add($tri1);
$container1->add($tri2);
$container2->add($tri3);
$container3->add($container1);
$container3->add($container2);
$container3->draw();
?>
The above example will output:
I'm a container 3.
I'm a container 1.
-I'm a triangle 1.
-I'm a triangle 2.
I'm a container 2.
-I'm a triangle 3.
Dario Ocles.
10-May-2007 01:49
uvillaseca at yahoo dot es :
Yes. In PHP singleton are dependent to a start page - unless if php is runt from a console - thus :
<?php
final class Foo {
private __construct() {
// read some static file like configuration file
}
public static function getInstance() {
static $instance = null;
if (null === $instance) $instance = new Foo();
return $instance;
}
}
?>
The file is read each time someone hit the startpage (say "index.php" which would include "Foo.php", and which would call Foo::getInstance()).
Use of cache like APC may allow you to achieve a singleton which would work on the whole server instead of each incoming connexion (an incoming connexion that read a php page, like index.php, will run the script, use the singleton, read the static file, each time while if the singleton was stored like a global instance among each incoming transaction, then the file would be read only once).
However, it is hard to do some as you are not in Java and J2EE, where such is valid and thread safe :
<?php // note : this is java!
public final class Foo {
private Foo() {/*read our file*/}
private static final Foo instance = new Foo();
public static final Foo getInstance() {
return instance;
}
}
?>
Because when Java load the class, a lock is acquired so that we only have one call to the constructor, this is equivalent :
<?php // note : this is java!
public final class Foo {
private static final Object synchrotron = new Object();
private Foo() {/*read our file*/}
private static Foo instance;
public static final Foo getInstance() {
synchronized (synchrotron) {
// force the thread to wait here
// eg: only one thread is in that block
if (instance == null) {
instance = new Foo();
}
return instance;
}
}
}
?>
The comparison with Java is useful, because it means that you have some work to do to achieve a singleton that would work on the whole server instead of only each incoming connexion :
If you use APC, like this :
<?php
final class Foo {
private __construct() {
// append "blah" to a file
}
public static function getInstance() {
if (false === ($o = apc_fetch(__FUNCTION__)) {
$o = new Foo();
apc_store(__FUNCTION__, $o);
}
return $o;
}
}
?>
This would work. But, when two incoming connexion arise at the same exact moment, then you will append "blah" twice.
As for the one that said that singleton are a class with static method, no it is not. A singleton is polymorphic, where static method are simply the equivalent with a class namespace of function.
If you have a method or function that expect an object implementing interface A, then you will be able to use singleton implementing interface A.
25-Mar-2007 01:12
Here's a way to make singleton classes with no extra code other than "extends Singleton" - if you use __AutoLoad() and keep each class in a separate file that is...
<?php
abstract class Singleton {
// Cannot be instantiated by any other class
protected function __Construct() { }
// Cannot be cloned ever
private function __Clone() { }
// Create/fetch an instance of the class
static public function getInstance($ClassName = null) {
static $Instance;
if ($Instance) {
return $Instance;
} elseif ($ClassName) {
$Instance = new $ClassName;
return $Instance;
} else {
return null; // or throw exception if you want...
}
}
}
function __AutoLoad($ClassName) {
include "$ClassName.php";
if (is_subclass_of($ClassName, 'Singleton')) {
eval($ClassName.'::getInstance($ClassName);');
}
}
// ClassName.php
class ClassName extends Singleton {
// ... Code ...
}
// To use
$a = ClassName::getInstance();
$a->MyFunc();
?>
Of course you could do this differently - the point is that you can use __AutoLoad() to tell the single what class it is, unlike some other ways where this is done manually every time you define a singleton class.
28-Dec-2006 10:45
Thanks for the example below of an extendable Singleton. I have enhanced it a little so the derived class does not need to implement any functions. The technique to get an instance is now
$x = Singleton::connect( "derived_class_name")
<?php
class Singleton
{
/*
How to use:
extend new class from singleton
To connect:
$x = Singleton::connect( "CLASSNAME" ) ;
*/
private static
$instanceMap = array();
//protected constructor to prevent outside instantiation
protected function __construct()
{
}
//deny cloning of singleton objects
public final function __clone()
{
trigger_error('It is impossible to clone singleton', E_USER_ERROR);
}
public function connect( // return a (reference to) a single instance of className
$className // name of a class derived from Singleton
)
{
// see if class already constructed
if(!isset(self::$instanceMap[$className]))
{
// if not then make one
$object = new $className;
//Make sure this object inherit from Singleton
if($object instanceof Singleton)
{
// save in static array
self::$instanceMap[$className] = $object;
}
else
{
throw SingletonException("Class '$className' do not inherit from Singleton!");
}
}
// return reference to single instanace of className
return self::$instanceMap[$className];
}
}
?>
<?php
class A extends Singleton
{
protected $rndId;
protected function __construct()
{
$this->rndId = rand();
}
public function whatAmI()
{
echo 'I am a ' . get_class( $this ) . ' (' . $this->rndId . ')<br />';
}
}
class B extends A
{
}
$a = Singleton::connect( "A" );
$b = Singleton::connect( "B" );
$a->whatAmI();// should echo 'I am a A(some number)
$b->whatAmI();// should echo 'I am a B(some number)
unset( $a ) ;
$c = Singleton::connect( "A" );
$d = Singleton::connect( "B" );
$c->whatAmI();// should echo 'I am a A(same number as above)
$d->whatAmI();// should echo 'I am a B(same number as above)
$a = new A();// this should fail
$b = new B();// this should fail
?>
06-Dec-2006 07:48
A pure extendable(multiple sublevels) Singleton class in PHP seems impossible at the moment because of the way PHP handles static inheritance, but my class is close to what i want.
Very simple usage:
<?php
class Test extends Singleton {
public static function getInstance(){
return Singleton::getSingleton(get_class());
}
}
?>
Singleton class implementation:
<?php
class Singleton {
/***********************
* HOW TO USE
*
* Inherit(extend) from Singleton and add getter:
*
* //public getter for singleton instance
* public static function getInstance(){
* return Singleton::getSingleton(get_class());
* }
*
*/
private static $instanceMap = array();
//protected getter for singleton instances
protected static function getSingleton($className){
if(!isset(self::$instanceMap[$className])){
$object = new $className;
//Make sure this object inherit from Singleton
if($object instanceof Singleton){
self::$instanceMap[$className] = $object;
}
else{
throw SingletonException("Class '$className' do not inherit from Singleton!");
}
}
return self::$instanceMap[$className];
}
//protected constructor to prevent outside instantiation
protected function __construct(){
}
//denie cloning of singleton objects
public final function __clone(){
trigger_error('It is impossible to clone singleton', E_USER_ERROR);
}
}
?>
Just a simple test case:
<?php
class A extends Singleton {
protected $rndId;
protected function __construct(){
$this->rndId = rand();
}
public function whatAmI(){
echo 'I am a A('.$this->rndId.')<br />';
}
public static function getInstance(){
return Singleton::getSingleton(get_class());
}
}
class B extends A {
public function whatAmI(){
echo 'I am a B('.$this->rndId.')<br />';
}
public static function getInstance(){
return Singleton::getSingleton(get_class());
}
}
$a = A::getInstance();
$b = B::getInstance();
$a->whatAmI();// should echo 'I am a A(some number)
$b->whatAmI();// should echo 'I am a B(some number)
$a = A::getInstance();
$b = B::getInstance();
$a->whatAmI();// should echo 'I am a A(same number as above)
$b->whatAmI();// should echo 'I am a B(same number as above)
$a = new A();// this should fail
$b = new B();