Object-oriented cloning of PHP

Source: Internet
Author: User
Tags object copy return

One of the biggest drawbacks of PHP4 is that it treats objects as another data type, which makes many common OOP methods unusable, such as design patterns. These OOP methods rely on methods that pass objects as references to other classes, not as values. Luckily, PHP solves this problem. All objects are now treated as references by default. But because all object pairs are treated as references rather than values, it is more difficult to copy objects now. If you try to copy an object, this is the address that points to the original object. To address replication issues, PHP provides a way to clone display objects.

Examples are as follows:

First, use the Clone keyword to clone an object:


 
  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 (). "
";
    
    $test 2 = clone $test;
    $test 2->setname ("Liwei");
    echo $test->getname ();
    echo $test->getnum (). "
";
    
    echo $test 2->getname ();
    echo $test 2->getnum ();
    
? >
Run Result:
tian123456
tian123456
xia123456

From the results of the run we see that if Test2 does not modify name. Test and test2 are different objects but have the same properties, and changing the properties of the Test2 object does not affect the properties of the test object, so you can conclude that the clone is a value, not a simple reference.

PHP5 defines a special method name "__clone ()" method, is a method that is invoked automatically when an object is cloned, using the "__clone ()" method to create an object with the same properties and methods as the original object, and if you want to change the contents of the original object after cloning, you need to __clone () To rewrite the original properties and methods, the "__clone ()" method can have no parameters, it automatically contains the $this and $that two pointers,$this point to the copy , and the $that points to the original ;


 
  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 (). "
";
    
    $test 2 = clone $test;     $test 2->setname ("Xia");
    echo $test->getname ();
    echo $test->getnum (). "
";
    
    echo $test 2->getname ();
    echo $test 2->getnum ();
    
? >

Run Result:
tian123456
tian123456
huang123456


 
  name = $name;
        $this->sex = $sex;
        $this->age = $age;
    }
    This person can speak in a way that speaks his own property
    function say () {
        echo "My name is:". $this->name. "Sex:". $this->sex. "My Age is:". $this
        ->age. "
";
    }
    The method that is invoked automatically when the object is cloned, if you want to change the contents of the original object after cloning, you need to rewrite the original properties and Methods in __clone (). The
    function __clone () {
        //$this a copy of the P2, and $that is pointing to the original P1, so that in this method, the properties of the copy are changed.
        $this->name = "I am a copy of the John $that->name";
        $this->age =;
    }
$p 1 = new Person ("John", "Male");
$p 2 = clone $p 1;
$p 1->say ();
$p 2->say ();
? >

Results after run:

My name is: John Sex: Male My age is: My name is:
I am a copy of John Sex: Male My age is: 20



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.