: 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.