Php object-oriented $ this keyword usage details. This article will introduce you to the php class that we often use this keyword, next, I will briefly analyze the usage of the $ this keyword in php. I hope this tutorial will introduce this keyword to you in this article, next, I will briefly analyze the usage of the php object-oriented $ this keyword. I hope this tutorial will be helpful to you.
As we know before, the attributes and methods of accessing object members outside the object are completed by referencing the object. In the object, the member method accesses other member attributes or member methods of the object, and must be accessed in the form of a special object reference "$ this-> value, $ this indicates the object to which the member belongs and can only be used in the Member method of the object.
To solve the naming conflicts and uncertainty between variables and attributes in php classes and objects, the "$ this" keyword is introduced to call the current object.
You must use the "$ this->" keyword to call the attributes and methods of the current object in the class. $ this indicates the new object created by the constructor in the constructor; the local variables in the method do not belong to the object and do not use the $ this keyword. With the $ this keyword, we can call object attributes or methods in the class.
1. call variables
Instance:
| The code is as follows: |
|
Class user {
Private $ n;
Function _ construct (){
$ Name = "Mike ";
Echo $ this-> n = $ name;
}
}
$ P = new user ();
?>
|
2. call method
Instance:
| The code is as follows: |
|
|
Class cal {
Public function sum ($ a, $ B ){
Return $ a + $ B;
}
Public function prt ($ a, $ c ){
Return $ a * $ c;
}
Public function result ($ a, $ B, $ c ){
$ A = $ this-> sum ($ a, $ B );
Return $ this-> prt ($ a, $ c );
}
}
$ C = new cal ();
Echo "(2 + 3) * 10 =". $ c-> result ('2', '3', '10 ');
?> |
Let's take a look at the example below. What is $ this doing?
| The code is as follows: |
|
Class Person {
// The following are the member attributes of a person.
Var $ name; // name of a person
Var $ sex; // gender of a person
Var $ age; // age of a person
// Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
Function _ construct ($ name = "", $ sex = "", $ age = ""){
$ This-> name = $ name;
$ This-> sex = $ sex;
$ This-> age = $ age;
}
// This person can talk about his/her attributes.
Function say (){
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";
}
// Method automatically called during object cloning. to change the content of the original object after cloning, rewrite the original content in _ clone ().
Attributes and methods
Function _ clone (){
// $ This refers to the copy p2, and $ that points to the original p1, so that the attributes of the copy are changed in this method.
$ This-> name = "I am a fake $ that-> name ";
$ This-> age = 30;
}
}
$ P1 = new Person ("zhang san", "male", 20 );
$ P2 = clone $ p1;
$ P1-> say ();
$ P2-> say ();
?> |
Output in the preceding example:
Execution result
My name is John. Gender: Male. my age is: 20.
My name is: I am a fake gender of Michael Jacob. my male age is: 30.
Let's take a look at what $ this has done in the above code:
1. access members of an object, such as $ this-> name
2. Other methods for accessing objects, such as the $ this-> run () and $ this-> eat ("apple") methods outside of the say () method.
It is worth noting that local variables and member attributes can have the same name, but their scopes and access methods are different. for example, $ name in the eat () method is equivalent to a local variable, the function scope is limited to the eat method, while the $ name in the member attribute declaration of Person is equivalent to a global variable, which can be accessed in the form of $ this-> name in other methods.
Example
| The code is as follows: |
|
|
<? Php
Class UserName
{
// Define attributes
Private $ name;
// Define the constructor
Function _ construct ($ name)
{
$ This-> name = $ name;
// This pointer has been used here
}
// Destructor
Function _ destruct (){}
// Print the username member function
Function printName ()
{
Print ($ this-> name );
// The PHP keyword this pointer is used again.
}
}
// Instantiate the object
$ NameObject = new UserName
("Heiyeluren ");
// Execute print
$ NameObject-> printName ();
// Output: heiyeluren
// The second object instantiation
$ NameObject2 = new UserName ("PHP5 ");
// Execute print
$ NameObject2-> printName (); // output: PHP5
?> |
The keyword "else" is now available. I will briefly analyze the usage of the php object-oriented $ this keyword. I hope this tutorial will help you learn more...