-
-
class UserName - {
- //Define properties
- private $name;
- //Define constructor
- function __construct( $name ){
- $this->name = $name; //This pointer has been used here
- }
//Destructor
- function __destruct(){}
- //Print user name member function
- function printName(){
- print( $this->name ); //The 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" );
- //Perform printing
- $nameObject2-> ;printName(); //Output: PHP5
- ?>
-
-
Copy code
The above class uses this pointer on lines 11 and 20 respectively, so who is this pointing to at that time? Woolen cloth?
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 example, print( $this->name ) becomes print( $nameObject2->name ), so "PHP5" is output. Therefore, this is a pointer to the current object instance and does not point to any other object or class.
(2)self
First of all, make it clear that self points to the class itself, that is, self does not point to any instantiated object. Generally, self is used to point to static variables in the class.
-
-
- class Counter
- {
- //Define attributes, including a static variable
- private static $firstCount = 0;
- private $lastCount;
- < ;p>//Constructor function
- function __construct(){
- $this->lastCount = ++selft::$firstCount; //Use self to call static variables. When calling self, you must use:: (field operation symbol)
- }
//Print the last count value
- function printLastCount(){
- print( $this->lastCount );
- }
- }
/ /Instantiate the object
- $countObject = new Counter();
- $countObject->printLastCount(); //Output 1
- ?>
-
Copy the code
Pay attention to two places: Lines 6 and 12.
A static variable $firstCount is defined in the second line, and the initial value is 0. Then this value is called on line 12, using self to call, and using "::" to connect in the middle, which is the so-called domain. operator, then what is called at this time is the static variable $frestCount defined by the class itself. The static variable has nothing to do with the instance of the following object. It is only related to the class. Then when I call the class itself, I cannot use this to reference it. I can use self. To reference, because self points to the class itself and has nothing to do with any object instance.
In other words, if you want to use static members in the class, you must also use self to call it.
(3)、parent
We know that parent is a pointer to the parent class, and parent is generally used to call the constructor of the parent class.
-
-
//Base class - class Animal
- {
- //Attributes of the base class
- public $name; //Name
- //Constructor of the base class
- public function __construct( $name ){
- $this->name = $name;
- }
- }
//Derived class
- class Person extends Animal //Person class inherits Animal class
- {
- public $personSex; //Gender
- public $personAge; //Age
- //Inherited class constructor
- function __construct( $personSex, $personAge ){
- parent::__construct( "heiyeluren" ); //Use parent calls the constructor of the parent class
- $this->personSex = $personSex;
- $this->personAge = $personAge;
- }
- function printPerson(){
- print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );
- }
- }
//Instantiate the Person object
- $personObject = new Person( "male", "21");
- //Perform printing
- $personObject->printPerson(); //Output: heiyeluren is male, this year 21
- ?>
-
Copy code
Attention to details:
Member attributes are all public, especially those of the parent class, for inherited classes to access through this.
Note the key points:
Line 25: parent:: __construct( "heiyeluren" ), then we use parent to call the constructor of the parent class to initialize the parent class, because the members of the parent class are all public, so we can inherit Use this directly in the class to call.
|