当前位置: 移动技术网 > IT编程>开发语言>PHP > php设计模式 Prototype (原型模式)代码

php设计模式 Prototype (原型模式)代码

2019年04月21日  | 移动技术网IT编程  | 我要评论

人生得意无南北,上海yy房产网,huainei

复制代码 代码如下:

<?php
/**
* 原型模式
*
* 用原型实例指定创建对象的种类.并且通过拷贝这个原型来创建新的对象
*
*/
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()."<br/>";
$objprototype2 = $objprototype;
echo $objprototype2->getid()."<br/>";
$objprototype3 = $objprototype->getclone();
echo $objprototype3->getid()."<br/>";

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网