PHP class Constant Usage example Analysis _php skill

Source: Internet
Author: User
Tags php class php programming

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.

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.