-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathprototype.php
More file actions
64 lines (52 loc) · 1.05 KB
/
prototype.php
File metadata and controls
64 lines (52 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* prototype pattern
* @author: 0xnz
* @update: Fri Sep 27 22:43:15 CST 2013
*/
interface Prototype {
public function copy();
}
class ConcretePrototype implements Prototype {
private $_name;
public function __construct($name) {
$this->_name = $name;
}
public function setName($name) {
$this->_name = $name;
}
public function getName() {
return $this->_name;
}
public function copy() {
/**
* 深拷贝实现
*/
/*
$serialize_obj = serialize($this); //序列化
$clone_obj = unserialize($serialize_obj); //反序列化
return $clone_obj;
*/
return clone $this; //浅拷贝
}
}
/* 测试深拷贝用的引用类 */
class Demo {
public $array;
}
class Client {
public static function main() {
$demo = new Demo();
$demo->array = array(1, 2);
$obj1 = new ConcretePrototype($demo);
$obj2 = $obj1->copy();
var_dump($obj1->getName());
var_dump($obj2->getName());
$demo->array = array(3, 4);
var_dump($obj1->getName());
var_dump($obj2->getName());
var_dump(($obj1 == $obj2));
}
}
Client::main();
?>