PHP--Magic Method Object cloning: __clone ()

Source: Internet
Author: User

Methods for generating objects:

1, from the class to produce the object. New, open up a memory space through new to the heap area

2. Object generation from Object. clone, through the keyword clone, a complete copy of an object, a new piece of memory space, the copy of the results stored in the memory

Syntax: New object (variable) = Clone old object (variable)

Object cloning: __clone ()

When an object is clone, the cloned object automatically calls the __clone () method, and the cloning method does not go through the construction method

<?PHP//Object Cloning    classperson{//Properties         Public $name; Private $age; //Static Properties         Public Static $counts= 0; //Method         Public function__construct ($name,$age){            $this->name =$name; $this->age =$age; //CountSelf::$counts++; }         Public functionSetage ($age){            $this->age =$age; }         Public functionGetage () {return $this-Age ; }        //Cloning Magic Method         Public function__clone () {//CountSelf::$counts++; //Working with Objects            Var_dump($this); }    }    Echo' <pre> '; //instantiation of    $person=NewPerson (' Zhou Zhijo ', 15); Var_dump($person); //Cloning Objects    $person 1=Clone $person; Var_dump($person 1); //Modifying Objects    $person 1->name = ' Zhang Mowgli '; Var_dump($person 1); //Output Object    EchoPerson::$counts;

PHP--Magic Method Object cloning: __clone ()

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.