<?php
/**
* Rozszerzalny singleton.
*
* <b>Użycie</b>
* <ul><li>W każdej klasie pochodnej utwórz protected static $instance</li>
* <li>Konstruktor kazdej klasy pochodnej musi byc chroniony lub prywatny</li></ul>
*/
class Singleton
{
/** To jest do celow testowych */
protected $nazwa;
protected function __construct()
{
}
{
{
$className = get_called_class();
$className::$instance = new $className;
}
}
public function identify()
{
}
}
class Pierwsza extends Singleton
{
protected function __construct()
{
$this->nazwa = "pierwsza";
}
}
class Druga extends Singleton
{
protected function __construct()
{
$this->nazwa = "druga";
}
}
class Trzecia extends Druga
{
protected function __construct()
{
$this->nazwa = "trzecia";
}
}
$pierwsza = Pierwsza::get();
$druga = Druga::get();
$trzecia = Trzecia::get();
$trzecia->identify();
$pierwsza->identify();
$druga->identify();
?>
Jak go ulepszyc bym nie musial deklarowac w kazdej klasie public static $instance?
jesli usune deklaracje tych zmiennych statycznych z klas to wtedy tworzy mi singleton zamiast klasy tej co trzeba (IMG:
style_emoticons/default/biggrin.gif) (ew w przypadku klasy trzecia utworzy mi klase druga).
Ten post edytował Orzeszekk 7.11.2011, 11:46:45