Php const constant modifier usage

Source: Internet
Author: User
Tags define definition

In php, if we want to define constants, there are many methods, but in the class we want to define constants, most of them will use the const constant modifier to define them, the following small series will introduce the operation methods to you.

Defining constants in PHP is done through the define () function, but defining constants in the class cannot use define (), but the const modifier must be used. After a constant in a class is defined by const, its access method is similar to that of a static member. It is accessed by the class name or by using self in the member method, however, PHP 5.3.0 and later versions can also be accessed using objects. A constant defined by const cannot be assigned a value again. If you try to change its value in a program, an error will occur.

The Code is as follows: Copy code

<? Php

Class MyClass {

Const CONSTANT = 'constant value'; // use const to declare a CONSTANT and assign it to the initial value.

Function showConstant (){

Echo self: CONSTANT. "<br>"; // use self to access constants. Do not add "$" before constants"

}

}

Echo MyClass: CONSTANT. "<br>"; // use the class name to access a CONSTANT outside the class, and do not add "$"

$ Class = new MyClass ();

$ Class-> showConstant ();

Echo $ class: CONSTANT; // After PHP 5.3.0

?>

Follow-up details: you do not need to use the "$" symbol before using the constant name defined by const, and the constant name is usually in uppercase.

An error occurs when you try to assign a value to a constant defined by const.

The Code is as follows: Copy code

<? Php

Class MyClass {

Const CONSTANT = 'constant value ';

Function setCONSTANT (){

Self: CONSTANT = 'news constant'; // the program running result will fail.

}

}

Echo MyClass: CONSTANT;

?>

The program running result will be incorrect.


The constant modified with const is a little different from other constants, that is, do not use "$" before the constant name. Remember! Of course, this constant value cannot be modified. Once defined, it cannot be modified "manually" anywhere in the program. This is the same as the define definition, and the use of const to define it also complies with the naming rules of other constants.


Additional reading:

There is no dollar sign before the constant ($ );
Constants can only be defined using the define () function, but cannot be defined using the value assignment statement;
Constants can be defined and accessed anywhere, regardless of the variable range rules;
Once defined, a constant cannot be redefined or canceled;
The constant value can only be a scalar;
Constants can only contain scalar data (boolean, integer, float, and string). Do not define resource constants.
You can use the constant () function to read the value of a constant. get_defined_constants () to obtain a list of all defined constants.
If an undefined CONSTANT is used, PHP assumes that it wants the name of the CONSTANT, just like calling it with a string (CONSTANT corresponds to "CONSTANT "), an E_NOTICE error is reported.
PHP's "Magic constant ".

Name

Description

_ LINE __

The current row number in the file.

_ FILE __

The complete file path and file name. If it is used in a include file, the include file name is returned. Since PHP 4.0.2,_ FILE __It always contains an absolute path, and earlier versions sometimes contain a relative path.

_ FUNCTION __

Function Name (New PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters.

_ CLASS __

Class Name (New PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, the value is always lowercase letters.

_ METHOD __

Class Method Name (New PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).

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.