- < ? PHP
- Class UserName
- {
- Defining properties
- Private $name;
- Defining constructors
- function __construct ($name)
- {
- $this- > name = $name;
The this pointer is already used here
- }
- Destructors
- function __destruct () {}
- Print User name member function
- function Printname ()
- {
- Print ($this->name);
and using the PHP keyword this pointer
- }
- }
- Instantiating an Object
- $ Nameobject = New UserName
("Heiyeluren");
- Perform printing
- $nameObject- > Printname ();
Output: Heiyeluren
- Second instantiation of the object
- $ NameObject2 = New UserName ("PHP5");
- Perform printing
- $nameObject 2- > printname ();//output: PHP5
- ?>
Let's see, the class above uses the this pointer in 11 rows and 20 rows, so who is this at that point? In fact this is at the time of instantiation to determine who to point to, such as when the first instantiation of the object (25 rows), then this is pointing to the $nameobject object, then the execution of 18 lines of printing when the print ($this , name), Then of course the output is "Heiyeluren".
In the second instance, print ($this->name) becomes print ($nameObject 2->name), so it outputs "PHP5". So the PHP keyword this is a pointer to the current object instance and does not point to any other object or class.
http://www.bkjia.com/PHPjc/445969.html www.bkjia.com true http://www.bkjia.com/PHPjc/445969.html techarticle php classusername {//define attribute Private$name;//define constructor Function__construct ($name) {$this-name = $name;//Use this pointer} destructor Function_ ...