assessmentee.com

Home | Start test | Contact
 

Activerecord | Adapter | Decorator | DependencyInjectionContainer | Facade | Factory | Hydration | Inheritance | Iterator | Mapper | MVC | Observer | Prototype | Proxy | Registry | ServiceLocator | Singleton | Specification | Strategy | TableGateway | ZF1_TableGateway | ZF2_TableGateway |




    <?php
/*
 * Registry design pattern
 * implementation of singleton pattern
 */

class Store {

    private static $instance = null;
    private $items = array();

     private function __construct(){}

     public function getInstance(){
        if(is_null(self::$instance)){
            self::$instance = new store();
        }
        return self::$instance;
    }

    public function set($key, $value){
         $this->items[$key] = $value;
    }

    public function get($key){
        if(array_key_exists($key, $this->items)){
	    return $this->items[$key];
	}
    }
}


$store = Store::getInstance();
$store->set('special offer','peanuts');
$store->set('employee of the week','Martha');
echo 'Our special offer is '.$store->get('special offer').'!';


?>


    

Outputs:


Our special offer is peanuts!



 

© 2018 assessmentee.com