1. Instantiated objects When a class is defined, we use the New keyword to generate an object. $ Object name = new class name (), example: [php] <span style= "color: #333333;" ><?php class person { //below is the Member attribute var $name; //person's name V AR $sex; //man's sex var $age; //People's age //below is the member method function say ()//This person can speak the way { echo "This man is Talking"; } & nbsp function run ()//This person can walk the way { echo "This person is walking"; } } $p 1=new persons (); $p 2=new person (); ></span><span style= "color: #ff0000;" > </span> 2. Accessing members $p 1=new person () in an object; How to use the members of the object? To access members of an object, you use a special operator "->" to complete the access of an object member, similar to the "." In Java. Example: [php] <?php class person { //below is the Member attribute var $name; &nbs P //person's name var $sex; &nbsP //person sex var $age; //person's age //below is the person's member method function say ()///This person can speak the way { echo "This man is Talking"; } function run ()//This person can walk the way { echo "This person is walking"; } } $p 1=new persons (); Create an Instance object $p1 $p 2=new person (); Create an Instance object $p2 $p 3=new person (); Create instance Object $P3 //The following three lines are assigned to $p1 object properties $p 1->name= "John"; $p 1->sex= "male"; $p 1->age=20; //The following three lines are access to $p1 object's properties echo "P1 object's name is:". $p 1->name. " <br> "; echo "The gender of the P1 object is:". $p 1->sex. " <br> "; echo "The Age of the P1 object is:". $p 1->age. " <br> "; //The following two lines access the methods in the $p1 object $p 1->say (); $p 1->run (); //The following three lines are assigned to the $p2 object property $p 2->name= "Dick"; $p 2->sex= "female"; $p 2->age=30; //The following three lines are access to $p2 object's properties echo "P2 object's name is:". $p 2->name. " <br> "; echo "The gender of the P2 object is:". $p 2->sex. " <br> "; echo "The Age of the P2 object is:". $p 2->age. " <br> "; //BelowTwo rows accessing the methods in the $p2 object $p 2->say (); $p 2->run (); //The following three lines are assigned to the $p3 object property $p 3->name= "Harry"; $p 3->sex= "male"; $p 3->age=40; //The following three lines are access to the properties of the $p3 object echo "P3 object's name is:". $p 3->name. " <br> "; echo "The gender of the P3 object is:". $p 3->sex. " <br> "; echo "The age of the P3 object is:". $p 3->age. " <br> "; //The following two lines access the methods in the $p3 object $p 3->say (); $p 3->run (); ?> 3. Special reference to the use of "$this" PHP provides me with a reference to this object $this, each object has an object in the reference $this to represent this object, to complete the call of the object's internal members, this is the meaning of "this", the above example, We instantiate three instance objects $p1, $P 2, $P 3, each of these three objects have a $this representing object $p1, $p 2, $p 3 respectively. $this is the reference within an object that represents the object, in the same way that it is used within the object and the members of the calling object outside the object. $this-> Properties $this->name; $this->age; $this->sex; $this-> method $this->say (); $this->run (); Example: [PHP] <?php classPerson { //below is the Member attribute var $name //The name of the var $sex; //person's Gender VAR $age; //person's age //below is the person's member method function say ()///This person can speak the way { , echo "My name. Called: "$this->name." Sex: "$this->sex." My Age is: ". $this->age." <br> "; } function run ()//The way this person can walk { echo "This man is Walking"; } } $p 1=new person (); Create an Instance object $p1 $p 2=new person (); Create an Instance object $p2 $p 3=new person (); Create instance Object $P3 //The following three lines are assigned to $p1 object properties $p 1->name= "John"; $p 1->sex= "male"; $p 1->age=20; //below access to the $p1 object in the speech method $p 1->say (); //The following three lines are assigned to the $p2 object property $p 2->name= "Dick"; $p 2->sex= "female"; $p 2->age=30; //below access to the $p2 object in the speech method $p 2->say (); //The following three lines are assigned to the $p3 object property $p 3->name= "Harry"; $p 3->sex= "male"; $p 3->age=40; //below access to the $p3 object in the speech method $p 3->say (); ?> Output: My name is: John Sex: Male My age is: 20 my name: Dick Sex: Female my age is: 30 my name: Harry Sex: Male My age is:
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.