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
/*
 * Singleton design pattern / the lazy variation
 */

class Sales {

    private static $instance = null;
    private $sales = array();
    // block direct instantiation, set visibility to private
    private function __construct(){}

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

    public function sale($sale){
        array_push($this->sales,$sale);
    }
    public function salesReport(){
        foreach($this->sales AS $sale){
            echo sprintf('<p> Sold a %s</p>', $sale);
        }
    }

}
Sales::getInstance()->sale('banana');
Sales::getInstance()->sale('apple');
Sales::getInstance()->salesReport();
?>

    

Outputs:


Sold a banana

Sold a apple



 

© 2018 assessmentee.com