Home > php教程 > php手册 > body text

PHP面向对象之克隆

WBOY
Release: 2016-06-13 09:10:17
Original
1231 people have browsed it

PHP面向对象之克隆

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

实例如下:

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

 

<!--?php
    class TestClone
    {
    	public  $name;
    	public $num;
    	
    	function setName($na)
    	{
    		$this--->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();
    
?>
Copy after login
运行结果:

tian123456
tian123456
xia123456
Copy after login

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

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

<!--?php
    class TestClone
    {
    	public  $name;
    	public $num;
    	
    	function setName($na)
    	{
    		$this--->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();
    
?>
Copy after login

运行结果:

tian123456
tian123456
huang123456
Copy after login

<!--?php
class Person {
    // 下面是人的成员属性
    var $name; // 人的名字
    var $sex; // 人的性别
    var $age; // 人的年龄
              // 定义一个构造方法参数为属性姓名$name、性别$sex 和年龄$age 进行赋值
              // function __construct($name="", $sex="",$age="")
    function __construct($name, $sex, $age) {
        $this--->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 ();
?>
Copy after login

运行后的结果:

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

 

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!