php const常量修饰符使用方法
在php中如果我们要定义常量有很多方法,但在类中我们要定义常量多半会用到const常量修饰符来定义了,下面小编来给各位同学介绍一下操作办法。
在PHP中定义常量是通过define()函数来完成的,但在类中定义常量不能使用define(),而需要使用const修饰符,类中的常量使用const定义后,其访问方式和静态成员类似,都是通过类名或在成员方法中使用self访问,但在PHP 5.3.0之后也可以使用对象来访问,被const定义的常量不能重新赋值,如果在程序中试图改变它的值将会出现错误,实例代码如下:
<?php class MyClass { const CONSTANT = 'CONSTANT value'; //使用const声明一个常量,并直接赋上初使值 function showConstant() { echo self::CONSTANT . "<br>"; //使用self访问常量,注意常量前不要加"$" } } echo MyClass::CONSTANT . "<br>"; //在类外部使用类名称访问常量,也不要加"$" $class = new MyClass(); $class->showConstant(); echo $class::CONSTANT; // PHP 5.3.0之后 ?>
关注细节:使用const定义的常量名称前不需要使用“$“符号,且常量名称通常都是大写的。
试图为const定义的常量赋值,将会出现错误,代码如下:
<?php class MyClass { const CONSTANT = 'CONSTANT value'; function setCONSTANT() { self::CONSTANT = 'news CONSTANT'; //程序运行结果将会出错。 } } echo MyClass::CONSTANT; ?>
程序运行结果将会出错,使用const修饰的常量更其它的常量,有点不同的地方就是:常量名前不要使用”$”,切记!当然这个常量值也是不能修改的,一旦定义就不能程序的任何地方进行“人为”的修改,这跟使用 define定义是一样,还有就是使用const来定义当然也遵守其它常量的命名规则.
扩展阅读:
常量前面没有美元符号($);
常量只能用 define() 函数定义,而不能通过赋值语句;
常量可以不用理会变量范围的规则而在任何地方定义和访问;
常量一旦定义就不能被重新定义或者取消定义;
常量的值只能是标量;
常量只能包含标量数据(boolean,integer,float 和 string),不要定义 resource常量.
可以用函数 constant() 来读取常量的值.get_defined_constants()可以获得所有已定义的常量列表.
如果使用了一个未定义的常量,PHP 假定想要的是该常量本身的名字,如同用字符串调用它一样(CONSTANT 对应 “CONSTANT”),此时将发出一个 E_NOTICE 级的错误.
PHP 的“魔术常量”.
名称 |
说明 |
__LINE__ |
文件中的当前行号。 |
__FILE__ |
文件的完整路径和文件名。如果用在包含文件中,则返回包含文件名。自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径,而在此之前的版本有时会包含一个相对路径。 |
__FUNCTION__ |
函数名称(PHP 4.3.0 新加)。自 PHP 5 起本常量返回该函数被定义时的名字(区分大小写)。在 PHP 4 中该值总是小写字母的。 |
__CLASS__ |
类的名称(PHP 4.3.0 新加)。自 PHP 5 起本常量返回该类被定义时的名字(区分大小写)。在 PHP 4 中该值总是小写字母的。 |
__METHOD__ |
类的方法名(PHP 5.0.0 新加)。返回该方法被定义时的名字(区分大小写)。 |
本文地址:
转载随意,但请附上文章地址:-)

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
