- < ?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 username member function
- function printName()
- {
-
print( $this- >name );
//The PHP keyword this pointer is used again
- }
- }
- //Instantiated object
-
$nameObject = new UserName
( "heiyeluren" );
- //Perform printing
-
$nameObject->printName();
//Output: heiyeluren
- //Second instantiation of the object
-
$nameObject2 = new UserName( "PHP5" );
- // Execute printing
-
$nameObject2->printName(); //Output :PHP5
-
?>
Let’s see, the above class uses this pointer on lines 11 and 20 respectively, so who does this point to at that time? In fact, this determines who it points to when instantiating it. For example, when the object is instantiated for the first time (line 25), then this points to the $nameObject object. Then when printing on line 18, print( $this ->name ), then of course "heiyeluren" is output.
In the second instance, print( $this->name ) becomes print( $nameObject2->name ), so "PHP5" is output. Therefore, 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.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445969.htmlTechArticle?php classUserName { //Define attributes private$name; //Define constructor function__construct($name) { $ this- name =$name; //This pointer has been used here} //Destructor function_...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn