Php object-oriented programming example study notes

Source: Internet
Author: User
: This article mainly introduces the learning notes for php object-oriented programming examples. For more information about PHP tutorials, see. 1. _ get () and _ set () methods

             
                         
 "; If (isset ($ this-> $ property_name) {return $ this-> $ property_name;} else {return NULL;} public function _ set ($ propertyname, $ value) {echo "this _ set () method is automatically called to assign values to private properties when setting private property values directly.
"; $ This-> $ propertyname = $ value ;}$ pig = new animal (); $ pig-> name =" "; $ pig-> color = "white"; $ pig-> age = "1 years old"; echo "name :". $ pig-> name."
"; Echo" color: ". $ pig-> color ."
"; Echo" age: ". $ pig-> age ."
";?>

2. _ call () method

             
                         
 \ N ";}}$ test = new Test (); $ test-> demo (" one "," two "," three "); echo" this is a test
";?>

3. clone object

             
                         
 Initial value $ this-> name = $ name; // assign the initial value $ this-> color = $ color to the member attributes through the $ color passed in by the constructor; // assign the initial value $ this-> age to the member attribute $ this-> age through the $ age passed in by the constructor $ this-> age = $ age;} function getInfo () {echo 'the animal name is '. $ this-> name. ', the animal color is '. $ this-> color. ', the animal age is '. $ this-> age. '. ';}}$ pig = new animal ("pig", "White", "1 year old"); // clone the new object pig2 by using clone, the same attributes and methods as $ pig objects $ pig2 = clone $ pig; $ pig2-> getInfo ();?>

4. _ clone () method

             
                         
 Initial value $ this-> name = $ name; // assign the initial value $ this-> color = $ color to the member attributes through the $ color passed in by the constructor; // assign the initial value $ this-> age to the member attribute $ this-> age through the $ age passed in by the constructor $ this-> age = $ age;} function getInfo () {echo 'the animal name is '. $ this-> name. ', the animal color is '. $ this-> color. ', the animal age is '. $ this-> age. '. ';} function _ clone () {// $ this indicates the duplicate pig2, and $ that indicates the original pig, so that the duplicate attribute is changed in this method; $ this-> name = "fake $ this-> name"; $ this-> age = '2 year'; }}$ pig = new animal ("pig ", "White", "1 year old "); // Use clone to clone pig2, which has the same attributes and methods as $ pig. $ pig2 = clone $ pig; $ pig-> getInfo (); $ pig2-> getInfo ();?>

5. _ toString () method

             
                         
 foo = $foo;            }                        public function __toString() {                return $this->foo;            }        }        $class = new TestClass('HelloWorld');        echo $class;        ?>    

6. const keywords

             
                         
 showConstant();        ?>    

The above introduces the php object-oriented programming example study notes, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.