This article introduces the content of the PHP design model of the prototype model, has a certain reference value, now share to everyone, the need for friends can refer to
prototype mode (Prototype) is not very difficult to understand.
Summing up is to create objects in a cloned way, saving the complexity of creating objects.
It can be used primarily to prevent a large amount of code redundancy resulting from duplicate creation of objects.
The recording code is as follows:
<?php /** Abstract Prototype class * Abstract class Prototype */abstract class Prototype { abstract function Cloned (); } /** Hero class Inherits abstract class * Hero * / class Hero extends Prototype {public $weapon;//Weapon Variable function Create () { echo "hero has been created, holding"; } Clone method function Cloned () { return clone $this;//PHP Clone method } }
<?php Header ("Content-type:text/html;charset=utf-8"); ------------------prototype mode index.php------------------ require_once "prototype.php"; Instantiate the Hero class as a hero $hero 1 = new Hero (); $hero 1->weapon = "glass harp"; Add weapon //second time by cloning a hero to get number second $hero 2 = $hero 1->cloned (); $hero 1->create (); echo "=>{$hero 1->weapon}<br/>"; Cloning copies the functions and variables inside the class $hero 2->create (); echo "=>{$hero 2->weapon}<br/>";
The output is:
The hero has been created, the hand is holding the
The hero has been created, the hand is holding the
Related recommendations:
PHP design mode single-instance mode
Abstract Factory of PHP design pattern
The factory method of PHP design mode