Back to Top

How to define PHP Magic constants

PHP provides a number of predefined constants.PHP constants with the two underscores (__) are called the magic methods, which are not actually constants but has the major role in PHP.The magic constants are mostly used to get the information about the current environment.If some things are not often used, it is easy to forget that but using magic constant, you can make some special features in PHP very easily.

Constant, name itself says it is a constant means can’t change it is a value and the magic constant name says it is a specific constant to create magic in PHP.PHP offers some predefined constants which can we use in many PHP scripts.A good PHP developer always develops clean and easily understandable code and you can make this thing possible with the help of Magical Constants.

There are eight magic constants provided by PHP like __LINE__, __FILE__ etc

NOTE: These Magical constants are case-insensitive

SYNTAX:

Let’s see about PHP Magic Constants one by one,

1. __FILE__

Display the full path and filename of the file.It is very useful to find the path of your file.If used within the included file, It returns the included file name.__ FILE__ contains an absolute path of the file.

The dirname (__ FILE__) function returns the directory path to the file in PHP.

2. __LINE__

Display current line number of the file.It is very helpful constant for debugging purpose.With this, you can easily get details about the line numbers in files.

3. __DIR__

Display current directory of the file.Its really very helpful PHP magic constant because its give directory path or can say absolute path so we can easily sort out the path of directories. It is equivalent to dirname (__ FILE__) function.

4. __FUNCTION__

Display function name and added in PHP 4.3.0. It return the name of Function means when the function was defined is and returned result would be case-sensitive. In PHP 4, it values is lowercase.

Let’s take an idea about this Magic constant.

5. __CLASS__

Display the class name in a case-sensitive manner.It’s one of the interesting Magic constant because it returns class name as a result.

Here are the few other articles which you might like

Static Keyword in PHP
Type Hinting in PHP5
To Reset the WordPress Loop

6. __METHOD__

Display the method name of the class.One of my favorite and powerful Magic constant because its returns declared method name of the class. so as PHP developer, it is not a good constant for us?

7. __NAMESPACE__

Display the name of the current namespace and its newly added feature in PHP 5.3

8. __TRAIT__

Last but not the least a TRAIT a new magical constant.it will display the trait name and it is added in new PHP in PHP 5.4

NOTE: PHP 5.4 is not stable yet, So you can visit this link to run the TRAIT code in PHP 5.4

Overall try to use magic constants in your PHP application.Do let us know if you face any issues while using magic constants in PHP application.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use CSS3 Object-fit

Posted on 9 years ago

Bhumi

Storing Objects in Html5 Local Storage

Posted on 8 years ago

Bhumi

How to use Namespace in PHP

Posted on 12 years ago

Bhumi