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 ()