PHP object-oriented constant in PHP5

Source: Internet
Author: User
Tags constant parse error php tutorial

<?
// 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.

 

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.