Defining constants in PHP is done through the Define () function, but defining constants in a class cannot use define (), and you need to use the const modifier. Constants in a class are defined by using const, and they are accessed in the same way as static members, either through the class name or by using self in the member method, but can be accessed after PHP 5.3.0. Constants defined by const cannot be assigned a value, and an error occurs if you try to change its value in your program.
| The code is as follows |
Copy Code |
|
<?php
Class MyClass {
Const CONSTANT = ' CONSTANT value '; Declare a constant by using const and assign the first value directly to the
function Showconstant () {
echo Self:: CONSTANT. " <br>//Use Self to access constants, note constants without "$"
}
}
echo MyClass:: CONSTANT. "<br>"; Use class names to access constants outside of a class, and do not add "$"
$class = new MyClass ();
$class->showconstant ();
echo $class:: CONSTANT; After PHP 5.3.0
?>
|
Attention to detail: you do not need to use the "$" symbol before using the constant name defined by const, and constant names are usually uppercase.
An error occurs when you try to assign a value to a constant that is defined by Const.
| The code is as follows |
Copy Code |
|
<?php
Class MyClass {
Const CONSTANT = ' CONSTANT value ';
function SetConstant () {
Self:: CONSTANT = ' news CONSTANT ';//program run result will be wrong.
}
}
echo MyClass:: CONSTANT;
?>
|
The result of the program running will be wrong.
Constants modified by the const some of the other constants are a little different: do not use the "$" before the constant name, remember! Of course, this constant value can not be modified, once the definition can not be anywhere in the program for "artificial" modification. This is the same as using the define definition, as well as naming rules that use const to define and, of course, follow other constants
Extended reading:
Constants are not preceded by a dollar sign ($);
Constants can only be defined with the Define () function, not by assignment statements;
Constants can be defined and accessed anywhere, regardless of the variable-scope rules;
Constants cannot be redefined or redefined once they are defined;
The value of a constant can only be a scalar;
Constants can only contain scalar data (boolean,integer,float and string) and do not define resource constants.
You can use the function constant () to read the value of a constant. Get_defined_constants () Gets a list of all the defined constants.
If you use an undefined constant, PHP assumes that you want the name of the constant itself, as if it were called with a string (CONSTANT corresponds to "CONSTANT"), and a e_notice level error is emitted.
The "Magic Constants" of PHP.
|
Name |
Description |
|
__line__ |
The current line number in the file. |
|
__file__ |
The full path and file name of the file. If used in the include file, the include file name is returned. Since PHP 4.0.2,__file__ always contains an absolute path, and the previous version sometimes contains a relative path. |
|
__function__ |
Function name (PHP 4.3.0 new). From PHP 5 This constant returns the name (case-sensitive) of the function when it is defined. This value is always lowercase in PHP 4. |
|
__class__ |
The name of the class (PHP 4.3.0 new). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). This value is always lowercase in PHP 4. |
|
__method__ |
The method name of the class (PHP 5.0.0 new). Returns the name (case-sensitive) of the method when it is defined. |