When designing web applications, it often makes sense conceptually and architecturally to allow access to one and only one instance of a particular class. The singleton pattern enables us to do this.
The singleton pattern is useful when we need to make sure we only have a single instance of a class for the entire request lifecycle in a web application. This typically occurs when we have global objects (such as a Configuration class) or a shared resource (such as an event queue).Reference: www.phptherightway.com
PROJECT_ROOT/vendor/PHPluz/Singleton.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?php namespace PHPluz; /** * Class Singleton restricts the instantiation of a class to one object. * * Singleton pattern is useful when exactly one object is needed to coordinate * actions across the system. The concept is sometimes generalized to systems * that operate more efficiently when only one object exists. * Reference: http://www.phptherightway.com/pages/Design-Patterns.html * * @package PHPluz * * @author Janis Jekabsons <php@pluz.de> * @copyright 2016 Janis Jekabsons * @license https://opensource.org/licenses/MIT (MIT) * @version 1.0 */ abstract class Singleton { /** @var array $instance Array containing all the classes that have been instantiated */ protected static $instance = array(); /** * The constructor __construct() is declared as protected to prevent creating * a new instance outside of the class via the new operator. */ protected function __construct() { } /** * If the single instance does not exist, create it. * Return the single instance then. * * @return object Called Class */ final public static function getInstance() { $calledClassName = get_called_class(); if (! isset (self::$instance[$calledClassName])) { self::$instance[$calledClassName] = new $calledClassName(func_get_args()); } return self::$instance[$calledClassName]; } /** * The magic method __clone() is declared as private to prevent cloning * of an instance of the class via the clone operator. * * @return void */ final private function __clone() { } /** * The magic method __wakeup() is declared as private to prevent unserializing * of an instance of the class via the global function unserialize() . * * @return void */ final private function __wakeup() { } } |
Example:
PROJECT_ROOT/vendor/PHPluz/etc/examples/Singleton/SingletonDemo.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php namespace PHPluz\Examples\Singleton; use PHPluz\Singleton; /** * Class SingletonDemo * * Demo for the Singleton class * * @package PHPluz\Examples\Singleton * * @author Janis Jekabsons <php@pluz.de> * @copyright 2016 Janis Jekabsons * @license https://opensource.org/licenses/MIT (MIT) * @version 1.0 */ class SingletonDemo extends Singleton { /** @var mixed $var */ private $var; /** * SingletonDemo constructor. * * @param array|null $options */ protected function __construct($options = null) { $this->var = null; } /** * Function getVar * * @return mixed */ public function getVar() { return $this->var; } /** * Function setVar * * @param mixed $foo */ public function setVar($foo) { $this->var = $foo; } } |
Now let’s create a test to have some output.
PROJECT_ROOT/vendor/PHPluz/etc/examples/Singleton/index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php namespace PHPluz\Examples\Singleton; use \Autoload; /** * index.php * * Example for the Singleton class * * @package PHPluz\Examples\Singleton * * @author Janis Jekabsons <php@pluz.de> * @copyright 2016 Janis Jekabsons * @license https://opensource.org/licenses/MIT (MIT) * @version 1.0 */ // needed to autoload the classes require_once '../../../../../Autoload.php'; Autoload::getInstance(); $lineFeed = "<br>\n"; print_r('Get Instance of class SingletonDemo: $foo = SingletonDemo::getInstance();' . $lineFeed); $foo = SingletonDemo::getInstance(); print_r('$foo->getVar() :: Expecting result => NULL' . $lineFeed); var_dump($foo->getVar()); print_r($lineFeed . 'Set $var in SingletonDemo to "buz": $foo->setVar("buz")' . $lineFeed); $foo->setVar("buz"); print_r('$foo->getVar() :: Expecting result => string(3) "buz"' . $lineFeed); var_dump($foo->getVar()); print_r($lineFeed . 'Get Instance of class SingletonDemo into new variable: $bar = SingletonDemo::getInstance();' . $lineFeed); $bar = SingletonDemo::getInstance(); print_r('$bar->getVar() :: Expecting result => string(3) "buz"' . $lineFeed); var_dump($bar->getVar()); |
Output from the code above:
1 2 3 4 5 6 7 8 9 |
Get Instance of class SingletonDemo: $foo = SingletonDemo::getInstance(); $foo->getVar() :: Expecting result => NULL NULL Set $var in SingletonDemo to "buz": $foo->setVar("buz") $foo->getVar() :: Expecting result => string(3) "buz" string(3) "buz" Get Instance of class SingletonDemo into new variable: $bar = SingletonDemo::getInstance(); $bar->getVar() :: Expecting result => string(3) "buz" string(3) "buz" |