简介:这是php设计模式 Prototype (原型模式)的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。
class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=339371' scrolling='no'>1 <?php
2 /**
3 * 原型模式
4 *
5 * 用原型实例指定创建对象的种类.并且通过拷贝这个原型来创建新的对象
6 *
7 */
8 abstract class Prototype
9 {
10 private $_id = null;
11
12 public function __construct($id)
13 {
14 $this->_id = $id;
15 }
16
17 public function getID()
18 {
19 return $this->_id;
20 }
21
22 public function __clone() // magic function
23 {
24 $this->_id += 1;
25 }
26
27 public function getClone()
28 {
29 return clone $this;
30 }
31 }
32
33 class ConcretePrototype extends Prototype
34 {
35 }
36
37 //
38 $objPrototype = new ConcretePrototype(0);
39 $objPrototype1 = clone $objPrototype;
40 echo $objPrototype1->getID()."<br/>";
41 $objPrototype2 = $objPrototype;
42 echo $objPrototype2->getID()."<br/>";
43
44 $objPrototype3 = $objPrototype->getClone();
45 echo $objPrototype3->getID()."<br/>";