Constants
May 30, 2017 · View on GitHub
General
A constant is a named value. Once defined, the value of the constant can not be changed.
A constant can be defined in one of two ways: as a c-constant using a const-declaration or via a string literal, or as a d-constant by calling the library function define. However, the two approaches differ slightly. Specifically:
- The name of a c-constant must comply with the lexical grammar for a name while that for a d-constant can contain any source character.
- The name of a c-constant is case-insensitive while that for a
d-constant can be case-sensitive or case-insensitive based on the
value of the third argument passed to
define. - If
defineis able to define the given name, it returnstrue; otherwise, it returnsfalse.
The library function defined reports if a given name (specified as a string) is defined as a constant. The library function constant returns the value of a given constant whose name is specified as a string.
Examples
class C {
const float MAX_HEIGHT = 10.5; // define two (case-insensitive) c-constants
const float UPPER_LIMIT = MAX_HEIGHT;
}
define('COEFFICIENT_1', 2.345, true); // define a case-insensitive d-constant
define('FAILURE', true, false); // define a case-sensitive d-constant
Context-Dependent Constants
The following constants—sometimes referred to as magic constants—are automatically available to all scripts; their values are not fixed:
| Constant Name | Description |
|---|---|
__CLASS__ | string; The name of the current class. From within a trait method, the name of the class in which that trait is used. If the current namespace is other than the default, the namespace name and "\" are prepended, in that order. If used outside all classes, the value is the empty string. |
__DIR__ | string; The directory name of the script. A directory separator is only appended for the root directory. |
__FILE__ | string; The full name of the script. |
__FUNCTION__ | string; Inside a function, the name of the current function exactly as it was declared, with the following prepended: If a named namespace exists, that namespace name followed by "". If used outside all functions, the result is the empty string. For a method, no parent-class prefix is present. (See __METHOD__ and anonymous functions.) |
__LINE__ | int; the number of the current source line |
__METHOD__ | string; Inside a method, the name of the current method exactly as it was declared, with the following prepended, in order: If a named namespace exists, that namespace name followed by ""; the parent class name or trait name followed by ::. If used outside all methods, the result is the same as for __FUNCTION__. |
__NAMESPACE__ | string; The name of the current namespace exactly as it was declared. For the default namespace, the result is the empty string. |
__TRAIT__ | string; The name of the current trait. From within a trait method, the name of the current trait. If used outside all traits, the result is the empty string. |
Constant names beginning with __ are reserved for future use by the Engine.
Core Predefined Constants
The following constants are automatically available to all scripts:
| Constant Name | Description |
|---|---|
E_ALL | int; All errors and warnings, as supported. |
E_COMPILE_ERROR | int; Fatal compile-time errors. This is like an E_ERROR, except that E_COMPILE_ERROR is generated by the scripting engine. |
E_COMPILE_WARNING | int; Compile-time warnings (non-fatal errors). This is like an E_WARNING, except that E_COMPILE_WARNING is generated by the scripting engine. |
E_CORE_ERROR | int; Fatal errors that occur during Hack's initial start-up. This is like an E_ERROR, except that E_CORE_ERROR is generated by the core of Hack. |
E_CORE_WARNING | int; Warnings (non-fatal errors) that occur during Hack's initial start-up. This is like an E_WARNING, except that E_CORE_WARNING is generated by the core of Hack. |
E_DEPRECATED | int; Deprecation notices. Enable this to receive warnings about code that will not work in future versions. |
E_ERROR | int; Fatal run-time errors. These indicate errors that cannot be recovered from, such as a memory allocation problem. Execution of the script is halted. |
E_NOTICE | int; Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script. |
E_PARSE | int; Compile-time parse errors. |
E_RECOVERABLE_ERROR | int; Catchable fatal error. It indicates that a probably dangerous error occurred, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handler (see the library function set_error_handler), the application aborts as it was an E_ERROR. |
E_STRICT | int; Have Hack suggest changes to the source code to ensure the best interoperability. |
E_USER_DEPRECATED | int; User-generated error message. This is like an E_DEPRECATED, except that E_USER_DEPRECATED is generated in Hack code by using the library function trigger_error. |
E_USER_ERROR | int; User-generated error message. This is like an E_ERROR, except that E_USER_ERROR is generated in Hack code by using the library function trigger_error. |
E_USER_NOTICE | int; User-generated warning message. This is like an E_NOTICE, except that E_USER_NOTICE is generated in Hack code by using the library function trigger_error. |
E_USER_WARNING | int; User-generated warning message. This is like an E_WARNING, except that E_USER_WARNING is generated in Hack code by using the library function trigger_error. |
E_WARNING | int; Run-time warnings (non-fatal errors). Execution of the script is not halted. |
E_USER_DEPRECATED | int; User-generated warning message. This is like an E_DEPRECATED, except that E_USER_DEPRECATED is generated in Hack code by using the library function trigger_error. |
INF | float; Infinity |
M_E | float; e |
M_PI | float; Pi |
NAN | float; Not-a-Number |
PHP_EOL | string; the end-of-line terminator for this platform. |
PHP_INT_MAX | int; the maximum representable value for an integer. |
PHP_INT_MIN | int; the minimum representable value for an integer. |
PHP_INT_SIZE | int; the number of bytes used to represent an integer. |
The members of the E_* family have values that are powers of 2, so
they can be combined meaningfully using bitwise operators.
User-Defined Constants
A constant may be defined inside a class or inside an interface.