Php constant usage example analysis _ PHP-php Tutorial

Source: Internet
Author: User
This article mainly introduces the usage of php constants. The example analyzes the concepts, features, and related usage skills of class constants in php. For more information, see the example in this article. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.