PHP Magic Constants


In developing process, if you want to store a value that will remain the same along your script’s execution, you can define a constant.

Also PHP has some magic constants like PHP_VERSION, __LINE__, __FILE__, E_ERROR, __DIR__, __FUNCTION__, __CLASS__, and __METHOD__, E_ALL and others wixh can help you in your application development.


From all predefined constans are eight that have different value depending on where they are used. This are named magic constants, and all have a specific naming convention lieke this __NAME__ .


1. The simplest magic constant is __LINE__ is the current line number of the file that is being parsed. Cand be used for debuging purposes or when you throw exceptions.


2. __FILE__ Display the full path and filename of the file. Usually is used with magic constant __LINE__ to give accurate informations when an exception is throwed.


3. __DIR__ The directory of the file.


4. __FUNCTION__ The function name.


5. __CLASS__ The class name.


6. __TRAIT__ The trait name (since PHP 5.4).


7. __METHOD__ The class method name. Display the method name of class.


8. __NAMESPACE__ The name of the current namespace.


Add comment


Warning: Unknown: write failed: Disk quota exceeded (122) in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0