Php constant usage instance analysis, php constant instance analysis
This example describes the usage of php constants. Share it with you for your reference. The details are as follows:
<? Php/*** PHP class constant ** the class constant belongs to the class itself. It does not belong to the object instance and cannot be accessed through the object instance * public, protected, private, static modifier * sub-classes can override constants in the parent class. You can use (parent: :) to call constants in the parent class * starting from PHP5.3.0. You can use a variable to dynamically call the class. However, the value of this variable cannot be a keyword (such as self, parent, or static ). */Class Foo {// The constant value can only be a scalar, string, bool, integer, float, or null. The constant const BAR = 'bar' can be initialized using the nowdoc structure '; public static function getConstantValue () {// you can use self or class name to access your own constants within the class. For the outside, you must 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 (); // The above output barclass Bar extends Foo {const BAR = 'foo'; // rewrite the parent class constant public static function getMyConstant () {return self: BAR ;} public static function getParentConstant () {return parent: BAR ;}} echo Bar: getMyConstant (); // fooecho Bar: getParentConstant (); // bar
I hope this article will help you with php programming.