php clone wakeup,php __clone实现

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 = ;

$this->account = clone $this->account; //不加这一句,account在clone是会只被复制引用,其中一个account的balance被修改另一个也同样会被修改

}

}

$person = new Person("peter", , new Account());

$person->setId();

$person2 = clone $person;

$person2->account->balance = ;

echo '

';

var_dump($person, $person2);

?>

结果

object(Person)# () {

["id":"Person":private]=>

int()

["name":"Person":private]=>

string() "peter"

["age":"Person":private]=>

int()

["account"]=>

object(Account)# () {

["balance"]=>

int()

}

}

object(Person)# () {

["id":"Person":private]=>

int()

["name":"Person":private]=>

string() "peter"

["age":"Person":private]=>

int()

["account"]=>

object(Account)# () {

["balance"]=>

int()

}

}

随机推荐

docker和shipyard使用问题

http://blog.csdn.net/cuisongliu/article/details/49178461 Docker启动如果随服务一起启动? docker run -ti -d --rest ...

hdu2030 汉字统计

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2030 解题思路:主要考察汉字的编码方式, 汉字机内码在计算机的表达方式的描述是,使用二个字节,汉字的每 ...

【JAVA、C++】LeetCode 019 Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

FastJson的使用方法总结

本文参考:http://www.tuicool.com/articles/zUbQfa 还可参考:https://www.iflym.com/index.php/code/alibaba-fastjs ...

cakephp recursive -1,0,1,2 速查

-1  : model本身 0   :model本身 + belongTo + hasOne 1   :model本身 + belongTo + hasOne + hasMany 2:  :model ...

Effective c++ 第一章 让自己习惯C++

条款 01:c++是一个语言联邦而不是一种单一的语言, 它包括: 1.C语言:没有模版.没有异常.没有重载…… 2.Object-Oriented C++:class.析构函数.构造函数.封装.继承. ...

c# 柱状图(转载)

// c# 显示柱状图 using System; using System.Data; using System.Configuration; using System.Web; using Sys ...

nginx : server_name localhost 和 chrome : Provisional headers are shown

问题相关问题现象:解决思路解决方案总结 问题相关 nginx : server_name localhost chrome : Provisional headers are shown 问题现象: ...

SSM实战——秒杀系统之创建项目、管理依赖、设计数据库

注:本项目使用Myeclipse开发. 一:项目创建 1:使用Myeclipse创建一个web project,命名为MySeckill,并转换为Maven项目. 2:创建项目文件目录如下: 上面四个 ...

【Git安装】centos安装git

1 yum install git 安装后的默认存放地点/usr/bin/git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值