Php constant usage instance analysis, php constant instance analysis. Php constant usage example analysis, php constant instance analysis this article describes the php constant usage. Share it with you for your reference. The details are as follows: php *** PHP class constant ** class constant belongs to php class 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 ,'
'; Echo Foo: BAR ,'
'; $ Obj = new Foo (); echo $ obj-> getConstant (),'
'; Echo $ obj-> getConstantValue (),'
'; 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.
The example in this article describes the usage of php constants. Share it with you for your reference. The details are as follows: php/*** PHP class constant ** class constant is...