I'm attaching a Singleton version I've put together. This raises a few
points for discussion. They are:
1) Should the initialise method be abstract
2) Should __clone be private or throw an exception.
The philosophy I've adopted is to restrict the functionality to within
this method hence the use of private and final keywords. This is not a
universally accepted approach so I feel others should vote on the
approach that is adopted.
graeme.
initialise(); }
// By forcing the use of an initialise method the singleton can be reinitialised
// Should it be abstract ??? vote required...
protected function initialise(){}
public final function reinitialise()
{
$this->initialise();
}
// Prohibit the cloning of singleton objects
private final function __clone()
{
// Vote required...
// made private so cloning can't occur
// or we could: throw new Exception("Can't clone a Singleton class.");
}
} // end of class Singleton
class MySingleton extends Singleton
{
private $int;
protected function initialise()
{
$this->int = 10;
}
function display()
{
echo $this->int++ ."\n";
}
}
$myS = MySingleton::getInstance("MySingleton");
$myS->display();
$myS->display();
$myS = Singleton::getInstance("MySingleton");
$myS->display();
$myS->display();
$copyS = $myS; // Take a copy & check there is still just one object
$copyS->display();
$myS->display();
$myS->reinitialise(); // will reset the Singleton object
$myS->display();
$myS->display();
/*$copyS = clone $myS; // Cloneing prohibited should cause a compile error
$copyS->display();
$myS->display();
$copyS->display();
$myS->display();
/* */
?>
_______________________________________________
agavi-dev mailing list
[email protected]
http://labworkz.com/cgi-bin/mailman/listinfo/agavi-dev