<?
// 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;
?>
Parse error: parse error in E: PHPProjects est. php Tutorial on line 17
<?
// 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;
?>
In PHP5, constants defined by const are different from those defined by variables. You do not need to add a $ modifier. 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.