This article mainly introduces the use of PHP class constants, examples of the concept of class constants in PHP, characteristics and related use of skills, the need for friends can refer to the following
The examples in this article describe the usage of PHP class constants. Share to everyone for your reference. Specific as follows:
A class constant belongs to the class itself, not to an object instance, and cannot be accessed through an object instance
Subclasses can override constants in the parent class by calling constants in the parent::)
From PHP5.3.0, a variable can be used to dynamically invoke a class. However, the value of the variable cannot be a keyword (such as self,parent or static).
Constant values can only be scalar, string,bool,integer,float,null, and can be initialized with the NOWDOC structure
<?php/** * PHP Class Constants * Class constants belong to the class itself, not an object instance, cannot be accessed through an object instance * cannot be used public,protected, private,static modifier * Subclasses can override constants in the parent class by calling the constants in the parent class from the (parent::)) * Since PHP5.3.0, a variable can be used to invoke the class dynamically. However, the value of the variable cannot be a keyword (such as self,parent or static). */class foo{//constant value can only be scalar, string,bool,integer,float,null, can be initialized with the nowdoc struct const BAR = ' Bar '; public static function Getconstantvalue () {//In the inside of a class can use self or the class name to access its own constants, external need to use the class name return self::bar; } public Function Getconstant () {return self::bar; }} $foo = ' foo '; echo $foo:: BAR, ' <br/> '; Echo foo::bar, ' <br/> '; $obj = new Foo (); Echo $obj->getconstant (), ' <br/> '; echo $obj->getconstantvalue (), ' <br/> '; echo foo::getconstantvalue ();//output Barclass Bar above Extends foo{const BAR = ' Foo ';//override parent class constant public static function Getmyconstant () {return self::bar; } public static function Getparentconstant () {return parent::bar; }}echo bar::getmyconstant (); Fooecho bar::getparentconstant (); Bar