原型模式
这个是原型模式的简单原理实现,还有一种是直接clone $class, 个人感觉没有封装,不太好, 还有继承一个抽象类的,原理是相同的

1 <?php 2 3 class Prototype 4 { 5 private $_num; 6 7 public function getPrototype(){ 8 return clone $this; 9 } 10 11 public function setNum($num) 12 { 13 $this->_num = $num; 14 } 15 16 public function getNum() 17 { 18 echo $this->_num,"<br/>"; 19 } 20 } 21 22 $a = new Prototype; 23 24 $b = $a->getPrototype(); 25 $b->setNum(3); 26 $b->getNum(); 27 28 $c = $a->getPrototype(); 29 $c->setNum(5); 30 $c->getNum(); 31 32 33 $b->getNum();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步