Home  >  Article  >  Backend Development  >  Code example of php prototype mode implementation

Code example of php prototype mode implementation

黄舟
黄舟Original
2017-03-17 09:53:561053browse

phpPrototype patternCode example of implementation

<?php
// 原型模式

class Obj
{
	private $name = &#39;obj&#39;;
}

class Prototype
{
	private $type = &#39;prototype&#39;;
	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(&#39;prototype&#39;);
$c = clone $p; //浅克隆
var_dump($c === $p); //false
var_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); //false
var_dump($p->getObj() === $dp->getObj()); //false

The above is the detailed content of Code example of php prototype mode implementation. For more information, please follow other related articles on the PHP Chinese website!

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