PHP object-oriented PHP5 constant _ PHP Tutorial

Source: Internet
Author: User
Tags parse error
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...

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.