PHP Object-oriented (OOP): Clone object __clone () method

Source: Internet
Author: User

Sometimes we need to use two or more of the same objects in a project, if you re-create the object using the "new" keyword, then assign the same attributes, it is cumbersome and error-prone, so it is necessary to completely clone an object exactly as it is, And after cloning, two objects do not interfere with each other.

In PHP4 we use the "clone" keyword to clone the object;

<?classperson{//The following is the person's member property    var $name;//the name of the man    var $sex;//Man's Sex    var $age;//man's age//define a constructor method parameter assigns a property name $name, gender $sex, and age $age    function__construct ($name= "",$sex= "",$age= "") {        $this->name=$name; $this->sex=$sex; $this->age=$age; }    //This person can speak in a way that speaks his own attributes    functionsay () {Echo"My name is called:".$this->name. "Gender:".$this->sex. "My Age is:".$this->age. "<br>"; }}$p 1=NewPerson ("Zhang San", "male", 20);//Clone a new object using "Clone" P2, with the same properties and methods as the P1 object. $p 2=Clone $p 1;$p 2-say ();?>

PHP4 defines a special method named "__clone ()" method, which is the method that is called automatically when the object is cloned, and the "__clone ()" method will establish an object that has 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 () Overriding the original properties and methods, the "__clone ()" method can have no parameters, it automatically contains $this and $that two pointers,$this point to the replica , and $that point to the original ;

<?classperson{//The following is the person's member property    var $name;//the name of the man    var $sex;//Man's Sex    var $age;//man's age//define a constructor method parameter assigns a property name $name, gender $sex, and age $age    function__construct ($name= "",$sex= "",$age= "") {        $this->name=$name; $this->sex=$sex; $this->age=$age; }    //This person can speak in a way that speaks his own attributes    functionsay () {Echo"My name is called:".$this->name. "Gender:".$this->sex. "My Age is:".$this->age. "<br>"; }    //The method that is called 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 ()    function__clone () {//$this refers to the replica P2, while $that is pointing to the original P1, so that in this method, the properties of the replica are changed.         $this->name = "I'm a fake.$that->name "; $this->age = 30; }}$p 1=NewPerson ("Zhang San", "male", 20);$p 2=Clone $p 1;$p 1-say ();$p 2-say ();?>

The above example outputs:

My name is called: Zhang San Sex: Male my age is: 20
My name is called: I am false zhang San Sex: Male my age is:

PHP Object-oriented (OOP): Clone object __clone () method

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.