The examples in this article describe the PHP class constant usage. Share to everyone for your reference. as follows:
<?php/** * PHP Class constant * * Class constants belong to the class itself, not to an object instance, not to an object instance. * Cannot be decorated with public,protected,private,static * Subclasses can override constants in the parent class by using the par ENT::) to invoke constants in the parent class from PHP5.3.0, you can use a variable 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, you may initialize constant const BAR = ' Bar ' with nowdoc structure;
The public static function Getconstantvalue () {//within the class can access its own constants with the self or class name, and the external need to return Self::bar with the class name;
The 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 (); All of the above output bar class Bar extends Foo {Const BAR = ' Foo ';//overriding parent class constant public static function Getmyconstant () {return
Self::bar;
The public static function, Getparentconstant () {return parent::bar; } echo bar::getmyconstant (); Foo echo bar::getparentconstant ();
Bar
I hope this article will help you with your PHP programming.