Singtory (Entry Nr. 378, by user 1 | edit) |
|
|
The class Singtory below is a PHP registry pattern that makes it extremely easy to use any class as a singleton.
You dont have to care for initialization or anything. Just use the methods of any class like this:
$singtory->Class->Method();
Singtory will care for everything else.
class Singtory
{
function __get($name)
{
if (!isset($this->$name)) $this->$name=new $name;
return $this->$name;
}
}
Here is an example:
class Animal
{
public $name="Standard Animal";
function hi()
{
echo "Hi, I'm $this->name";
}
}
class Cat extends Animal
{
public $name="Standard Cat";
}
class Dog extends Animal
{
public $name="Standard Dog";
}
$singtory=new Singtory;
$singtory->Dog->hi(); // Hi Im Standard Dog
$singtory->Cat->hi(); // Hi Im Standard Cat
|
|
|
Create a new entry at this position
|
|
|