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)
- PHP5.3 supports calling classes with a variable
- PHP5.6 supports assigning values with an expression
PHP类常量
andPHP静态属性
- 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