Home >Backend Development >PHP Tutorial >PHP Design Patterns Prototype Pattern
The content of this article is about the prototype mode of PHP design pattern, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
Prototype It’s not difficult to understand.
To sum up, it is to create objects through a cloning method to save the complexity of creating objects.
It can be mainly used to prevent a lot of code redundancy caused by repeatedly creating objects.
The recording code is as follows:
<?php /** 抽象原型类 * abstract Class Prototype */ abstract Class Prototype { abstract function Cloned(); } /** 英雄类 继承抽象类 * Class Hero */ class Hero extends Prototype { public $weapon; // 武器变量 function Create() { echo "英雄已经创建完成,手里拿着"; } // 克隆方法 function Cloned() { return clone $this;// PHP clone方法 } }
<br/>
weapon = "琉璃琴"; // 添加武器 // 第二次通过克隆一号英雄 得到二号英雄 $hero2 = $hero1->Cloned(); $hero1->Create(); echo "=>{$hero1->weapon}<br/>"; // 克隆 会将类里面的函数和变量复制一份 $hero2->Create(); echo "=>{$hero2->weapon}<br/>";
##The output result is:
The hero has been The creation is completed, holding the Liuli Qin in hand=>Liu Li Qin
The hero has been created, holding the Liu Li Qin
PHP design pattern singleton pattern
PHP design pattern abstract factoryfactory method of PHP design patternThe above is the detailed content of PHP Design Patterns Prototype Pattern. For more information, please follow other related articles on the PHP Chinese website!