PHP 面向对象 PHP5 中的常量_php基础
在PHP5中 const定义的常量与定义变量的方法不同,不需要加 $ 修饰符。const PI = 3.14; 这样就可以。
而使用const 定义的常量名称一般都大写,这是一个约定,在任何语言中都是这样。
如果定义的常量由多个单词组成,使用 _ 连接,这也是约定。
比如, MAX_MUMBER 这样的命名方式。一个良好的命名方式,是程序员必须注意的。
类中的常量使用起来类似静态变量,不同点只是它的值不能被改变。我们使用 类名::常量名 来调用这个常量。
//声明一个final类Math
class Math{
const PI = 3.14;
public function __toString(){
return "这是Math类。";
}
//这里写了一个算圆面积的方法.使用了Const常量,
//注意使用的方法,类似与静态变量.
public final function areaOfCircular($r){
return $r * $r * self::PI ;
}
public final function max($a,$b){
return $a > $b ? $a : $b ;
}
}
echo Math::PI ;
?>
程序运行结果:
尝试为const定义的常量赋值,将会出现错误。
//声明一个final类Math
class Math{
const PI = 3.14;
public function __toString(){
return "这是Math类。";
}
//这里写了一个算圆面积的方法.使用了Const常量,
//注意使用的方法,类似与静态变量.
public final function areaOfCircular($r){
return $r * $r * self::PI ;
}
public final function max($a,$b){
return $a > $b ? $a : $b ;
}
public function setPI($a){
self::PI = 3.1415;
}
}
echo Math::PI ;
?>
程序运行结果:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

In this chapter, we are going to learn the following topics related to routing ?

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Validator can be created by adding the following two lines in the controller.
