Php Object cloning method, php Object cloning
This example describes how to clone objects in php. Share it with you for your reference. The details are as follows:
<? Php // defines the class staff, including the property id and name class staff {private $ id; private $ name; function setID ($ id) {$ this-> id = $ id;} function getID () {return $ this-> id;} function setName ($ name) {$ this-> name = $ name;} function getName () {return $ this-> name ;}} // create a new staff object and initialize $ ee1 = new staff (); $ ee1-> setID ("145"); $ ee1-> setName ("Simon "); // clone a new object $ ee2 = clone $ ee1; // reset the ID value of the new object $ ee2-> setID ("146"); // output ee1 and Ee2 echo "ee1 ID :". $ ee1-> getID (). "<br>"; echo "ee1 Name :". $ ee1-> getName (). "<br>"; echo "ee2 ID :". $ ee2-> getID (). "<br>"; echo "ee2 Name :". $ ee2-> getName (). "<br>";?>
<? Php // defines the class staff, including the property id and name class staff {private $ id; private $ name; function setID ($ id) {$ this-> id = $ id;} function getID () {return $ this-> id;} function setName ($ name) {$ this-> name = $ name;} function getName () {return $ this-> name;} // here is _ clone function _ clone () {$ this-> id = $ this-> id + 1 ;}// create a new staff object and initialize $ ee1 = new staff (); $ ee1-> setID ("145"); $ ee1-> setName ("Simon"); // clone a new $ Ee2 = clone $ ee1; // reset the ID value of the new object // $ ee2-> setID ("146 "); // output ee1 and ee2 echo "ee1 ID :". $ ee1-> getID (). "<br>"; echo "ee1 Name :". $ ee1-> getName (). "<br>"; echo "ee2 ID :". $ ee2-> getID (). "<br>"; echo "ee2 Name :". $ ee2-> getName (). "<br>";?>
I hope this article will help you with php programming.