php 示例
最后更新于:2022-04-02 04:19:18
[TOC]
## 概述
使用示例: PHP 中提供立即可用的原型模式。 你可以使用 clone关键字创建一个对象的完整副本。 如果想要某个类支持克隆功能, 你需要实现 __clone方法
## 示例
### 概念示例
### 真是示例
';
main.php
``` component = clone $this->component; $this->circularReference = clone $this->circularReference; $this->circularReference->prototype = $this; } } class ComponentWithBackReference { public $prototype; public function __construct(Prototype $prototype) { $this->prototype = $prototype; } } function clientCode() { $p1 = new Prototype(); $p1->primitive = 245; $p1->component = new \DateTime(); $p1->circularReference = new ComponentWithBackReference($p1); $p2 = clone $p1; if ($p1->primitive === $p2->primitive) { echo "primitive 相等\n"; } else { echo "Primitive 不相等\n"; } if ($p1->component === $p2->component) { echo "component 相等\n"; } else { echo "component 不相等\n"; } if ($p1->circularReference === $p2->circularReference) { echo "circularReference 相等\n"; } else { echo "circularReference 不相等\n"; } if ($p1->circularReference->prototype === $p2->circularReference->prototype) { echo "prototype 相等\n"; } else { echo "prototype 不相等\n"; } } clientCode(); ```### 真是示例