Getting started with const in PHP

Source: Internet
Author: User

1. Do not want a member to be modified, you want the value to be constant

Attention:

The constant name does not precede the $

constants, when defined, need to be assigned an initial value and then cannot be changed

Does not require modifiers, the default public

2. Constants Assign initial values :

For example:

<?php

Class A

{

Const constant NAME = value;

}

Interface B

{

Const constant NAME = value;

}

?>

3. Access variables:

Class: Self:: constant name/class name:: Constant Name

Outside class: Class Name:: Constant Name

Case 1:

<?php

Class A

{

consttax_rate=123;//constants, when defined, need to be assigned an initial value, without the addition of modifiers, the default public

Public function Pay ($val)

{

return $val *a::tax_rate;

or return $val *self::tax_rate;

}

}

Interface B

{

Const xy=1234;

}

Class C implements B

{

Public Function Pay1 ($val)

{

return $val *b::xy;

}

}

$A 1=new A ();

The constant operation in the ECHO "class:". $A 1->pay (2);

ECHO "<BR>";

$C 1=new C ();

ECHO "The constant operation of the interface:". $C 1->pay1 (2);

ECHO "<BR>";

?>

Results :

Constant operations in the class: 246
Constant operation of the interface: 2468


This article is from "Uncle Wei Xiaobao" blog, please be sure to keep this source http://darmi.blog.51cto.com/11607923/1775271

Getting started with const in PHP

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.