php克隆一个类,PHP面向对象之克隆_php

php4面向对象最大的缺点之一,是将对象视为另一种数据类型,这使得很多常见的OOP方法无法使用,如设计模式。这些OOP方法依赖于将对象作为引用传递给其他的类的方法,而不是作为值传递。幸好PHP解决了这个问题。现在所有对象在默认情况下都被视为引用。但是因为所有对象对被视为引用而不是值,所以现在复制对象显得更难了。如果尝试复制一个对象,这是会指向原对象的地址。为了解决复制问题,PHP提供了一种克隆显示对象的方法。

实例如下:

首先介绍使用clone关键字克隆对象:

name = $na; } function getName() { return $this->name; } function setNum($nu) { $this->num = $nu; } function getNum() { return $this->num; } } $test = new TestClone(); $test->setName("tianxin"); $test->setNum(123456); echo $test->getName(); echo $test->getNum()." "; $test2 = clone $test; $test2->setName("liwei"); echo $test->getName(); echo $test->getNum()." "; echo $test2->getName(); echo $test2->getNum(); ?>

1

name=$na;}functiongetName(){return$this->name;}functionsetNum($nu){$this->num=$nu;}functiongetNum(){return$this->num;}}$test=newTestClone();$test->setName("tianxin");$test->setNum(123456);echo$test->getName();echo$test->getNum()."";$test2=clone$test;$test2->setName("liwei");echo$test->getName();echo$test->getNum()."";echo$test2->getName();echo$test2->getNum();?>

运行结果:

tian123456 tian123456 xia123456

1

tian123456tian123456xia123456

从运行结果中我们看到,如果test2不对name进行修改。test与test2这两个对象的虽然是不同的对象但是却有相同的属性,而且改变test2对象的属性并不会影响test对象的属性,因此可以断定克隆是传值,而不是简单的引用。

PHP5定义了一个特殊的方法名“__clone()”方法,是在对象克隆时自动调用的方法,用“__clone()”方法将建立一个与原对象拥有相同属性和方法的对象,如果想在克隆后改变原对象的内容,需要在__clone()中重写原本的属性和方法, ”__clone()”方法可以没有参数,它自动包含$this和$that两个指针,$this指向复本,而$that指向原本;

name = $na; } function getName() { return $this->name; } function setNum($nu) { $this->num = $nu; } function getNum() { return $this->num; } function __clone() { $this->name = "huang"; } } $test = new TestClone(); $test->setName("tian"); $test->setNum(123456); echo $test->getName(); echo $test->getNum()." "; $test2 = clone $test; // $test2->setName("xia"); echo $test->getName(); echo $test->getNum()." "; echo $test2->getName(); echo $test2->getNum(); ?>

1

name=$na;}functiongetName(){return$this->name;}functionsetNum($nu){$this->num=$nu;}functiongetNum(){return$this->num;}function__clone(){$this->name="huang";}}$test=newTestClone();$test->setName("tian");$test->setNum(123456);echo$test->getName();echo$test->getNum()."";$test2=clone$test;//     $test2->setName("xia");     echo $test->getName();     echo $test->getNum()." ";          echo $test2->getName();     echo $test2->getNum();      ?>

运行结果:

tian123456 tian123456 huang123456

1

tian123456tian123456huang123456

name = $name; $this->sex = $sex; $this->age = $age; } // 这个人可以说话的方法, 说出自己的属性 function say() { echo "我的名字叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this ->age . " "; } // 对象克隆时自动调用的方法, 如果想在克隆后改变原对象的内容,需要在__clone()中重写原来的属性和方法。 function __clone() { // $this 指的复本p2, 而$that 是指向原本p1,这样就在本方法里,改变了复本的属性。 $this->name = "我是复制的张三$that->name"; // $this->age = 30; } } $p1 = new Person ( "张三", "男", 20 ); $p2 = clone $p1; $p1->say (); $p2->say (); ?>

1

name=$name;$this->sex=$sex;$this->age=$age;}// 这个人可以说话的方法, 说出自己的属性     function say() {         echo "我的名字叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this         ->age . " ";     }     // 对象克隆时自动调用的方法, 如果想在克隆后改变原对象的内容,需要在__clone()中重写原来的属性和方法。     function __clone() {         // $this 指的复本p2, 而$that 是指向原本p1,这样就在本方法里,改变了复本的属性。         $this->name = "我是复制的张三$that->name";         // $this->age = 30;     } } $p1 = new Person ( "张三", "男", 20 ); $p2 = clone $p1; $p1->say (); $p2->say (); ?>

运行后的结果:

我的名字叫:张三 性别:男 我的年龄是:20 我的名字叫:我是复制的张三 性别:男 我的年龄是:20

1

我的名字叫:张三性别:男我的年龄是:20我的名字叫:我是复制的张三性别:男我的年龄是:20

http://www.gaodaima.com/51551.htmlPHP面向对象之克隆_php

欢迎大家阅读《PHP面向对象之克隆_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

原创文章,转载请注明: 转载自搞代码

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值