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

/**
 * prototype design pattern
 * generate new objects by cloning existing one
 * avoid expensive instantiation with "new"
 * magic method __clone() is optional
 */


class Prototype {
    public $name;
    public $quality;
    public $price;


    public function setName($name) {
       $this->name = $name;
    }

    public function setprice($price) {
       $this->price = $price;
    }

    public function setQuality($quality) {
       $this->quality = $quality;
    }

    public function getProperties(){
        $property = array();
        $property[] = $this->name;
        $property[] = $this->quality;
        $property[] = $this->price;
        echo implode(" ", $property).'</br>';
    }


    public function __clone(){
        // optionally modify cloned object at creation
    }
}

$a = new Prototype();
$a->setName('apple');
$a->setQuality('B');
$a->setprice(5);
$b = clone $a;
$b->setQuality('C');
$b->setprice(2);

$a->getProperties();
$b->getProperties();



    

Outputs:


apple B 5
apple C 2



 

© 2018 assessmentee.com