Home > Backend Development > PHP Tutorial > Code example of php prototype mode implementation

Code example of php prototype mode implementation

黄舟
Release: 2023-03-06 17:44:02
Original
1117 people have browsed it

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template