The PHPself keyword points to a class static variable. First, we need to make it clear that self points to the class itself, that is, the PHPself keyword does not point to any instantiated object. Generally, self points to static variables in the class .? Php
First, we need to make it clear that self points to the class itself, that is, the PHP self keyword does not point to any instantiated object. Generally, self points to static variables in the class.
- <? Php
- Class Counter
- {
- // Define attributes, including a static variable
- Private static $ firstCount = 0;
- Private $ lastCount;
- // Constructor
- Function _ construct ()
- {
- $ This->LastCount = ++ selft
: $ FirstCount;
// Use the PHP self keyword to call static variables and use self
The call must use: (domain operator number)
- }
- // Print the maximum value
- Function printLastCount ()
- {
- Print ($ this->LastCount );
- }
- }
- // Instantiate the object
- $ CountObject = new Counter ();
- $ CountObject->PrintLastCount ();
// Output 1
- ?>
Here, we only need to pay attention to two places: 6th rows and 12th rows. We defined a static variable $ firstCount in the second row, and the initial value is 0. Therefore, this value is called in the 12 rows using the PHP self keyword, in addition, we use ":" To connect, which is our so-called domain operator.
At this time, we call the static variable $ frestCount defined by the class. our static variable has nothing to do with the instance of the following object. it is only related to the class, so I call the class itself, therefore, we cannot use this for reference. we can use the PHP self keyword for reference, because self points to the class itself and has nothing to do with any object instance. In other words, we must also use self to call static members in our class.
The distinct self keyword does not point to any instantiated object. Generally, self is used to point to static variables in the class. ? Php...