可以在对象类中定义一个__clone()方法来调整对象的克隆行为。此方法的代码将在克隆操作期间执行。除了将所有现有对象成员复制到目标对象之外,还会执行__clone()方法指定的操作。下面修改Corporate_Drone类,增加以下方法:
?
|
1
2
3
|
function __clone() {
$this->tiecolor = "blue";
}
|
之后,创建一个新的Corporate_Drone对象,增加employeeid成员的值,克隆这个对象,然后输出一些数据,从而显示克隆对象的tiecolor确实是通过__clone()方法设置的。示例代码:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php
// Create new corporatedrone object
$drone1 = new corporatedrone();
// Set the $drone1 employeeid member
$drone1->setEmployeeID("12345");
// Clone the $drone1 object
$drone2 = clone $drone1;
// Set the $drone2 employeeid member
$drone2->setEmployeeID("67890");
// Output the $drone1 and $drone2 employeeid members
echo "drone1 employeeID: ".$drone1->getEmployeeID()."<br />";
echo "drone2 employeeID: ".$drone2->getEmployeeID()."<br />";
echo "drone2 tiecolor: ".$drone2->getTiecolor()."<br />";
?>
|
程序运行结果
?
|
1
2
3
|
drone1 employeeID: 12345
drone2 employeeID: 67890
drone2 tiecolor:
|
再来一个小例子:
?
|
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
|
<?php
class Fruit {
private $name = "水果";
private $color = "颜色";
public function setName($name){
$this->name = $name;
}
public function setColor($color){
$this->color = $color;
}
function showColor(){
return $this->color.'的'.$this->name."<br />";
}
function __destruct(){
echo "被吃掉了(对象被回收) <br />";
}
}
$apple = new Fruit();
$apple->setName("大苹果");
$apple->setColor("红色");
echo $apple->showColor();
$clone_apple = $apple;
$clone_apple->setName("小苹果");
$clone_apple->setColor("青色");
echo $clone_apple->showColor();
?>
|
上面只是将一个类赋值给另一个类,所以此时内存中仍是一个对象。
?
|
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
|
<?php
class Fruit {
private $name = "水果";
private $color = "颜色";
public function setName($name){
$this->name = $name;
}
public function setColor($color){
$this->color = $color;
}
function showColor(){
return $this->color.'的'.$this->name."<br />";
}
function __destruct(){
echo "被吃掉了(对象被回收) <br />";
}
function __clone(){
$this->name = "克隆水果";
}
}
$apple = new Fruit();
$apple->setName("大苹果");
$apple->setColor("红色");
echo $apple->showColor();
$clone_apple = clone $apple;
$clone_apple->setColor("青色");
echo $clone_apple->showColor();
?>
|
clone方法克隆出了一个新的类,所以此时内存中有两个对象。
php的__clone()方法对一个对象实例进行的浅复制,对象内的基本数值类型进行的是传值复制,而对象内的对象型成员变量,如果不重写__clone方法,显式的clone这个对象成员变量的话,这个成员变量就是传引用复制,而不是生成一个新的对象.如下面一个例子的第28行注释所说
?
|
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
|
<?php
class Account {
public $balance;
public function __construct($balance) {
$this->balance = $balance;
}
}
class Person {
private $id;
private $name;
private $age;
public $account;
public function __construct($name, $age, Account $account) {
$this->name = $name;
$this->age = $age;
$this->account = $account;
}
public function setId($id) {
$this->id = $id;
}
public function __clone() { #复制方法,可在里面定义再clone是进行的操作
$this->id = 0;
$this->account = clone $this->account; #不加这一句,account在clone是会只被复制引用,其中一个account的balance被修改另一个也同样会被修改
}
}
$person = new Person("peter", 15, new Account(1000));
$person->setId(1);
$person2 = clone $person;
$person2->account->balance = 250;
var_dump($person, $person2);
?>
|
输出:
复制代码 代码如下:
object(Person)#1 (4) { ["id":"Person":private]=> int(1) ["name":"Person":private]=> string(5) "peter" ["age":"Person":private]=> int(15) ["account"]=> object(Account)#2 (1) { ["balance"]=> int(1000) } } object(Person)#3 (4) { ["id":"Person":private]=> int(0) ["name":"Person":private]=> string(5) "peter" ["age":"Person":private]=> int(15) ["account"]=> object(Account)#4 (1) { ["balance"]=> int(250) } }
object(Person)#1 (4) { ["id":"Person":private]=> int(1) ["name":"Person":private]=> string(5) "peter" ["age":"Person":private]=> int(15) ["account"]=> object(Account)#2 (1) { ["balance"]=> int(1000) } } object(Person)#3 (4) { ["id":"Person":private]=> int(0) ["name":"Person":private]=> string(5) "peter" ["age":"Person":private]=> int(15) ["account"]=> object(Account)#4 (1) { ["balance"]=> int(250) } }
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 29
-
2025-05-25 105
-
2025-05-26 72
-
2025-05-29 42
-
2025-05-27 48
热门评论

