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