First, we need to make it clear that self is pointing to the class itself, that is, that the PHP own keyword does not point to any object that has already been instantiated, and that the normal use is to point to a static variable in the class.
- < ? PHP
- Class Counter
- {
- Defining attributes, including a static variable
- private static $ Firstcount = 0 ;
- Private $lastCount;
- constructor function
- function __construct ()
- {
- $this- > LastCount = ++selft
:: $firstCount;
Use the PHP self keyword to invoke the static variable, using the self
Call must use::(domain operator symbol)
- }
- Print the maximum number of times
- function Printlastcount ()
- {
- Print ($this->lastCount);
- }
- }
- Instantiating an Object
- $ Countobject = New Counter ();
- $countObject- > Printlastcount ();
Output 1
- ?>
We just need to pay attention to two places, lines 6th and 12th. We define a static variable $firstcount in the second row, and the initial value is 0, then it is worthwhile to call this in 12 rows, using the PHP self keyword to invoke, and the middle using "::" To connect, is what we call the domain operator.
So this time we call the class itself defines the static variable $ frestcount, our static variable is not related to the instance of the following object, it is only associated with the class, then I invoke the class itself, then we can not use this to reference, using the PHP self keyword to reference, Because self is pointing to the class itself, it is independent of any object instance. In other words, if the static members of our class, we must also use self to invoke.
http://www.bkjia.com/PHPjc/445966.html www.bkjia.com true http://www.bkjia.com/PHPjc/445966.html techarticle first, let's make it clear that self is pointing to the class itself, that is, that the PHP own keyword does not point to any object that has already been instantiated, and that the normal use is to point to a static variable in the class .