PHP Common Concepts Confuse (v) the difference between PHP class constants, static properties, and attributes

Source: Internet
Author: User
Tags php class

In the recent reading of the handbook, I found that PHP has a number of pits, a note will fall in, while looking at the content of these easy to confuse the record down.

Tips: It's best to look at the manual in English, because there are a number of things in the English manual that are not available in the Chinese manual (latest PHP)

    1. PHP5.3 supports calling classes with a variable
    2. PHP5.6 supports assigning values with an expression PHP类常量 andPHP静态属性
    3. PHP7.1 support for PHP类常量 increased access control
Brief introduction
    • PHP class constants: defined in the same way as constants
    • PHP Properties: Define the same way as variables
    • PHP static properties: can be defined as PHP properties, add a static
PHP Properties
    • Using -> the accessed variable
    • Subclasses can override variables of the parent class
    • Variables can be modified at any time
    • Using Access in a class $this
PHP class Constants
    • With :: access class constants, you can pass and 对象 get class constants.

        <?php  class test{      const AAAA = "BBB";  }  $test = new test;  echo test::AAAA;  //BBB  echo $test::AAAA;   //BBB
    • Subclasses can override the class constants of the parent class

        <?php  class test{      const AAAA = "BBB";  }  class test2 extends test{      const AAAA = "ccc";      public function gettest(){          return parent::AAAA;      }  }     $test = new test2;  echo test::AAAA;   //ccc  echo test2::AAAA;  //ccc  echo $test::AAAA;  //BBB  echo $test->gettest();  //BBB
    • Once a class constant is defined, it cannot be modified
    • Using Access in a class self::类常量

PHP Static Properties
    • Using :: access static properties, you can pass and 对象 get static properties.

       <?php class test{     public static $AAAA = "BBB"; } $test = new test; echo test::$AAAA;    //BBB echo $test::$AAAA;   //BBB
    • Subclasses can override static properties of the parent class

        <?php  class test{      public static $AAAA = "BBB";  }  class test2 extends test{      public static $AAAA = "CCC";  }  $test = new test2;  echo test2::$AAAA;   //CCC  echo $test::$AAAA;   //CCC
    • Class constants define the modifications that can be made at any time
    • Re-use in class self::$test to access

PHP Common Concepts Confuse (v) the difference between PHP class constants, static properties, and attributes

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.