php原型模式实现的代码实例
程序员文章站
2022-04-15 08:24:26
...
php原型模式实现的代码实例
<?php // 原型模式 class Obj { private $name = 'obj'; } class Prototype { private $type = 'prototype'; private $obj = null; public function construct($type = null) { $this->type = $type; $this->obj = new Obj(); } public function getType() { echoLine($this->type); } public function getObj() { return $this->obj; } } $p = new Prototype('prototype'); $c = clone $p; //浅克隆 var_dump($c === $p); //false var_dump($p->getObj() === $c->getObj()); //true // ================================================== // 深克隆 function deepClone($obj) { if(!is_object($obj)) return null; return unserialize( serialize($obj) ); } $dp = deepClone($p); var_dump($dp === $p); //false var_dump($p->getObj() === $dp->getObj()); //false
以上就是php原型模式实现的代码实例的详细内容,更多请关注其它相关文章!