PHP Object-oriented (OOP) programming: This special usage

Source: Internet
Author: User
Or the methods in the object to invoke other methods of this object. What do we do then? Because all of the members in the object are called with objects, including calls between the internal members of the object, in PHP you give me a reference to this object $this, each object has an object reference $this to represent the object. To complete the call of the inner member of the object, this is the meaning of "this", in 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.
As we can see from the figure above, $this is a reference to the object inside the object, in the same way that it is used within the object and the members of the calling object outside the object.
  code is as follows copy code
$this-> properties: $this->name; $this->age; $this->sex;
$this-> method: $this->say (); $this->run ();
Modify the example above to let everyone speak their name, gender and age:
  code is as follows copy code

<?php
Class person
{
 //is a member property
 var $name;//person's name
 var $sex;//person's sex
 var $age; /person's age

 //The following are people's member methods
 function Say () {//The way this person can speak
  echo "My name is called:". $this->name. "Sex:". $this->sex. "My Age is:". $this->age;
 }

 function Run () {//This person can walk
  echo "This man is Walking";
 }
}

$p 1 = new person ();//Create Instance Pair Like $p1
$p 2 = new person ();//Create 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 =;

//below access to $P1The speech method in the image
$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 =

//below to access the speech method in the $p2 object
$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 =;

//below to access the speech method in the $p3 object
$p 3->say ();
?

The output results are:

My name is called: John Sex: Male My age is: 20 my name: Dick Sex: Female my age is: 30 My name is: Harry Sex: Male My age is: 40

Analyze This method:

The code is as follows Copy Code

function say () {//The way this person can speak
echo "My name is called:". $this->name. "Sex:". $this->sex. "My Age is:". $this->age;
}

There are say () methods in the three objects of $P1, $p 2 and $p3. $this represent these three objects, call the corresponding property, print out the value of the property, which is the way to access the object's properties within the object, and it is possible to call run () in Say (). Use the $this->run () method in the Say () to complete the call.

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.