Copy code The code is as follows:
/**
* Prototype pattern
*
* Use a prototype instance to specify the type of object to be created. And create a new object by copying this prototype
*
*/
abstract class Prototype
{
private $_id = null;
public function __construct($id)
{
$this->_id = $id;
}
public function getID()
{
return $this->_id;
}
public function __clone() // magic function
{
$this->_id += 1;
}
public function getClone()
{
return clone $this;
}
}
class ConcretePrototype extends Prototype
{
}
//
$objPrototype = new ConcretePrototype(0);
$objPrototype1 = clone $objPrototype;
echo $objPrototype1->getID()."
";
$objPrototype2 = $objPrototype;
echo $objPrototype2->getID()."
" ;
$objPrototype3 = $objPrototype->getClone();
echo $objPrototype3->getID()."
";
The above introduces the prototype.js PHP design pattern Prototype prototype pattern code, including the content of prototype.js. I hope it will be helpful to friends who are interested in PHP tutorials.