PHP Self keyword pointer to class static variable _php tutorial

Source: Internet
Author: User

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.

  1. < ? PHP
  2. Class Counter
  3. {
  4. Defining attributes, including a static variable
  5. private static $ Firstcount = 0 ;
  6. Private $lastCount;
  7. constructor function
  8. function __construct ()
  9. {
  10. $this- > LastCount = ++selft
    :: $firstCount;
    Use the PHP self keyword to invoke the static variable, using the self
    Call must use::(domain operator symbol)
  11. }
  12. Print the maximum number of times
  13. function Printlastcount ()
  14. {
  15. Print ($this->lastCount);
  16. }
  17. }
  18. Instantiating an Object
  19. $ Countobject = New Counter ();
  20. $countObject- > Printlastcount ();
    Output 1
  21. ?>

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 .

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.