Sdfasd
<?php/** * prototype mode (deep copy + shallow copy) */declare (Strict_types=1);//Turn on strongly typed mode class copyobj{ public $array;} Abstract Prototype class abstract class prototype{ protected $attach; public function __construct (copyobj $obj) { $this->attach = $obj; } public function set ($name, $value) { $this $name = $value; } public function get ($name) { return $this $name; } public function getattach () { return $this->ATTACH;&NBSp; } abstract public function clone ();} Class deepcopy extends prototype{ public function clone () { $obj = clone $this; return $obj; }}class Shallowcopy extends prototype{ public function clone () { $serializeObj = serialize ($this); $obj = unserialize ($SERIALIZEOBJ); return $obj; }}
123
123
----------deep copy--------------$chairman = new Copyobj (); $chairman->array = [' xxx ', ' xxx ', ' Jiang Zemin ']; $DEEPCOPYOBJ 1 = new Deepcopy ($chairman); $DEEPCOPYOBJ 2 = $deepCopyObj 1->clone (); echo "<pre>"; Var_dump ($DEEPCOPYOBJ 1-> Getattach ()); Var_dump ($DEEPCOPYOBJ 2->getattach ());//Modify Object $chairman->array = [' xxx ', ' xxx '];var_dump ($ Deepcopyobj1->getattach ()); Var_dump ($DEEPCOPYOBJ 2->getattach ());
Asdsad
-------------shallow copy-------------$start = new Copyobj (); $start->array = [' Andy Lau ', ' Jacky ', ' Aaron Kwok ', ' Dawn ']; $DEEPCOPYOBJ 3 = new Shallowcopy ($start); $DEEPCOPYOBJ 4 = $deepCopyObj 3->clone (); Var_dump ($DEEPCOPYOBJ 3->getattach ()); Var_dump ($ Deepcopyobj4->getattach ());//Modify Object $start->array = [' Jay Chou '];var_dump ($deepCopyObj 3->getattach ()); Var_dump ( $DEEPCOPYOBJ 4->getattach ());
PHP design mode: Prototype mode