- <? 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
- ?>
We can see that the class above uses the this pointer in 11 rows and 20 rows respectively. Who is this pointing? In fact, this is used to determine who to point to during instantiation. For example, when the object is first instantiated (25 rows), then this is to point to the $ nameObject object, when printing 18 rows, the print ($ this-> <name) is changed to the print ($ nameObject-> name), and the "heiyeluren" is output ".
In the second instance, print ($ this-> name) is changed to print ($ nameObject2-> name), so "PHP5" is output ". Therefore, the PHP keyword "this" is the pointer to the current object instance and does not point to any other object or class.