PHP5 Constants in PHP object-oriented _php tutorial

Source: Internet
Author: User
Declaring 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 area of the circle. Const constants are used,
Note The method used, similar to the static variable.
Public final function Areaofcircular ($r) {
return $r * $r * self::P i;
}
Public final function Max ($a, $b) {
Return $a > $b? $a: $b;
}
Public function SETPI ($a) {
Self::P i = 3.1415;
}
}
echo Math::P i;
?>

Parse error:parse Error in e:phpprojectstest.php tutorial on line 17


Declaring 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 area of the circle. Const constants are used,
Note The method used, similar to the static variable.
Public final function Areaofcircular ($r) {
return $r * $r * self::P i;
}
Public final function Max ($a, $b) {
Return $a > $b? $a: $b;
}
}
echo Math::P i;
?>

The const defined constants in PHP5 are different from the methods that define variables, and do not require a $ modifier. Const PI = 3.14; That's all you can do.
Constant names that are defined with const are generally capitalized, which is a convention that is true in any language.
If you define a constant that consists of multiple words, use the _ connection, which is also the Convention.
For example, max_mumber such a naming method. A good naming method is what programmers must pay attention to.
A constant in a class is used like a static variable, except that its value cannot be changed. We use the class name:: Constant name to invoke this constant.


http://www.bkjia.com/PHPjc/444958.html www.bkjia.com true http://www.bkjia.com/PHPjc/444958.html techarticle ?//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 area of the circle. Using the CO NST 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.