PHP Tutorial-php Tutorial

Source: Internet
Author: User
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.

  1. <? Php
  2. Class Counter
  3. {
  4. // Define attributes, including a static variable
  5. Private static $ firstCount = 0;
  6. Private $ lastCount;
  7. // Constructor
  8. Function _ construct ()
  9. {
  10. $ This->LastCount = ++ selft
    : $ FirstCount;
    // Use the PHP self keyword to call static variables and use self
    The call must use: (domain operator number)
  11. }
  12. // Print the maximum value
  13. Function printLastCount ()
  14. {
  15. Print ($ this->LastCount );
  16. }
  17. }
  18. // Instantiate the object
  19. $ CountObject = new Counter ();
  20. $ CountObject->PrintLastCount ();
    // Output 1
  21. ?>

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...

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.