Now that we know how to access a member of an object, accessed by the "Object-> member," which is the form of accessing a member of an object outside of the object, if I want the method inside the object to access the properties of this object, 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.
$this-> Property: $this->name; $this->age; $this->sex;
$this-> Method: $this->say (); $this->run ();
Revise the example above to let everyone speak their name, gender and age:
<?php
class
person {
//below is the member attribute of the human var $name;//person's
name
var $sex;//person's sex
var $age;//person's age
/ /Below is a member of the human method
function say () {//The way this person can speak
echo "My name is:". $this->name. "Sex:". $this->sex. "My Age is:". $this->age;
}
function run () {//This person can walk the way
echo "This man is Walking";
}
$p 1 = new person ();
>name = "John";
$p 1->sex = "male";
$p 1->age =;
The following accesses the speech method in the $p1 object
$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 =;
The following accesses 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 =;
The following accesses 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:
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.
Articles that you may be interested in
- PHP Introduction to object-oriented tutorial recommended
- Deep php: object-oriented, pattern, and Practice (3rd edition). pdf download
- PHP Object-oriented law
- A method of a PHP Object-Array (Object-array), JSON-to-array (JSON-array)
- PHP using curl to implement multithreaded classes, PHP curl multi-Threaded download pictures
- Problem resolution in PHP about abstract (abstract) classes and abstract methods
- MySQL Command change table structure: Add, delete, modify fields, adjust field order
- Classic dialogue between programmers and testers. These are foreign programmers summed up the share, called the global GM?