Php object cloning method, php object cloning _ PHP Tutorial

Source: Internet
Author: User
Php implements object cloning, and php object cloning. Php implements object cloning. php object cloning this article describes how php implements object cloning. Share it with you for your reference. Details: php defines the class staff. the php package implements object cloning and 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 ()."
"; Echo" ee1 Name: ". $ ee1-> getName ()."
"; Echo" ee2 ID: ". $ ee2-> getID ()."
"; Echo" ee2 Name: ". $ ee2-> getName ()."
";?>
<? 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 object $ ee2 = clone $ ee1; // reset the ID value of the new object // $ ee2-> setID ("146"); // output ee1 and ee2 echo "ee1 ID :". $ ee1-> getID ()."
"; Echo" ee1 Name: ". $ ee1-> getName ()."
"; Echo" ee2 ID: ". $ ee2-> getID ()."
"; Echo" ee2 Name: ". $ ee2-> getName ()."
";?>

I hope this article will help you with php programming.

Examples in this article describes how php implements object cloning. Share it with you for your reference. The details are as follows: php // defines the class staff, where the package...

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.