A constant in PHP object-oriented PHP5. In PHP5, constants defined by const are different from those defined by variables. you do not need to add a $ modifier. ConstPI3.14. The constant names defined by const are generally capitalized. in PHP5, the constants defined by const are different from the variables defined by the method, and no $ modifier is required. Const PI = 3.14; then you can.
The constant names defined by const are generally capitalized. this is a convention, as is true in any language.
If the defined constant is composed of multiple words and uses _ join, this is also the convention.
For example, MAX_MUMBER. A good naming method must be noticed by programmers.
Constants in a class are similar to static variables, but their values cannot be changed. We use the class name: constant name to call this constant.
The code is as follows:
// Declare a final class Math
Class Math {
Const PI = 3.14;
Public function _ toString (){
Return "this is the Math class. ";
}
// Here is a method for calculating the circular area. The Const constant is used,
// Pay attention to the method used, similar to static variables.
Public final function areaOfCircular ($ r ){
Return $ r * self: PI;
}
Public final function max ($ a, $ B ){
Return $ a> $ B? $ A: $ B;
}
}
Echo Math: PI;
?>
Program running result:
The code is as follows:
3.14
An error occurs when you try to assign values to a constant defined by const.
The code is as follows:
// Declare a final class Math
Class Math {
Const PI = 3.14;
Public function _ toString (){
Return "this is the Math class. ";
}
// Here is a method for calculating the circular area. The Const constant is used,
// Pay attention to the method used, similar to static variables.
Public final function areaOfCircular ($ r ){
Return $ r * self: PI;
}
Public final function max ($ a, $ B ){
Return $ a> $ B? $ A: $ B;
}
Public function setPI ($ ){
Self: PI = 3.1415;
}
}
Echo Math: PI;
?>
Program running result:
The code is as follows:
Parse error: parse error in E: \ PHPProjects \ test. php on line 17
The constant defined by const is different from the method used to define a variable. you do not need to add a $ modifier. Const PI = 3.14; then you can. The constant names defined by const are generally capitalized...