Home  >  Article  >  Backend Development  >  prototype.js php design pattern Prototype prototype pattern code

prototype.js php design pattern Prototype prototype pattern code

WBOY
WBOYOriginal
2016-07-29 08:45:43979browse

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn