Php (11) object-oriented object instantiation through class
Source: Internet
Author: User
1. instantiate an object when the class is defined, we use the new keyword to generate an object. $ Object name = new class name (); example: [php] & lt; spanstyle = & quot; color: 333333; & quot; & gt; & lt ;? PhpclassPerson {The following is the member attribute var $ name of a person; 1. instantiate an object. when the class is defined, we use the new keyword to generate an object. $ Object name = new class name (); example: [php] 2. the $ p1 = new Person () member in the access object; how to use the object member? To access members of an object, you must use a special operator "->" to access the object members, similar to the "." example in java: [php] Name = "zhang san"; $ p1-> sex = "male"; $ p1-> age = 20; // the following three rows are the attributes of the $ p1 object. echo "the name of the p1 object is :". $ p1-> name."
"; Echo" the gender of the p1 object is: ". $ p1-> sex ."
"; Echo" the age of the p1 object is: ". $ p1-> age ."
"; // The following two rows access the method $ p1-> say (); $ p1-> run () in the $ p1 object (); // the following three rows assign $ p2 object attributes $ p2-> name = "Li Si"; $ p2-> sex = "female"; $ p2-> age = 30; // the following three rows are the attributes of $ p2 object. echo "p2 object name is :". $ p2-> name."
"; Echo" p2 object gender: ". $ p2-> sex ."
"; Echo" p2 object age: ". $ p2-> age ."
"; // The following two rows access the $ p2 object method $ p2-> say (); $ p2-> run (); // the following three rows assign $ p3 object attributes $ p3-> name = ""; $ p3-> sex = "male"; $ p3-> age = 40; // the following three rows are the attributes of the $ p3 object. echo "the name of the p3 object is :". $ p3-> name."
"; Echo" the sex of the p3 object is: ". $ p3-> sex ."
"; Echo" the age of the p3 object is: ". $ p3-> age ."
"; // The following two rows access the $ p3 object's method $ p3-> say (); $ p3-> run ();?> 3. the special reference "$ this" in PHP provides me with a reference to this object $ this. each object contains an object reference $ this to represent this object, to call the internal members of an object, this is meant to be "this". In the instance above, we instantiate three instance objects $ P1, $ P2, $ P3, each of these three objects has a $ this representing the objects $ p1, $ p2, and $ p3 respectively. $ This refers to the reference of this object internally. it is used in the same way as the members of the object called internally and the members of the object called externally. $ This-> attribute $ this-> name; $ this-> age; $ this-> sex; $ this-> method $ this-> say (); $ this-> run (); example: [php] Name. "Gender:". $ this-> sex. "My age is:". $ this-> age ."
";}Function run () // The way this Person can walk {echo" this Person is walking ";}}$ p1 = new Person (); // Create an instance object $ p1 $ p2 = new Person (); // Create an instance object $ p2 $ p3 = new Person (); // Create an instance object $ p3 // the following three rows assign $ p1 object attributes $ p1-> name = "Zhangsan"; $ p1-> sex = "male "; $ p1-> age = 20; // The method for accessing the $ p1 object $ p1-> say (); // the following three rows assign $ p2 object attributes $ p2-> name = "Li Si"; $ p2-> sex = "female"; $ p2-> age = 30; // the following method of accessing the $ p2 object $ p2-> say (); // the following three rows assign $ p3 object attributes $ p3-> name = "Wang Wu "; $ p3-> sex = "male"; $ p3-> age = 40; // The description in the $ p3 object accessed below Method $ p3-> say ();?> Output result: my name is Zhangsan Gender: Male and my age is: 20 my name is Li Si Gender: Female my age is: 30 my name is: wang Wu gender: my male age is: 40
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.