首页 > 学院 > 开发设计 > 正文

原型模式

2019-11-08 02:38:04
字体:
来源:转载
供稿:网友
<?php// 原型模式class Obj{	PRivate $name = 'obj';}class Prototype{	private $type = 'prototype';	private $obj = null;		public function __construct($type = null)	{		$this->type = $type;		$this->obj = new Obj();	}	public function getType()	{		echoLine($this->type);	}	public function getObj()	{		return $this->obj;	}}$p = new Prototype('prototype');$c = clone $p; //浅克隆var_dump($c === $p); //falsevar_dump($p->getObj() === $c->getObj()); //true// ==================================================// 深克隆function deepClone($obj){	if(!is_object($obj))		return null;	return unserialize( serialize($obj) );}$dp = deepClone($p);var_dump($dp === $p); //falsevar_dump($p->getObj() === $dp->getObj()); //false
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表