What are the PHP class constants? Class constants Usage Explained

Source: Internet
Author: User
Tags php class scalar
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 
Related Article

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.