PHP Magic Constants Explained

Like PHP Magic Methods we have number of Constants defined in PHP. PHP team has declared 8 of the constants as Magic Constants.

These constants generally starts and ends with __ (double underscore).

These constants provides different values like current file name, current class name, etc. And the value of this constants will change based on where those are used. Lets get into each one.

__LINE__

This will print the line number on which this constant is used. Have a look at the below code block.

echo __LINE__; #Print 1 echo __LINE__; #Print 2 echo __LINE__; #Print 3
 echo __LINE__; #Print 1
echo __LINE__; #Print 2
echo __LINE__; #Print 3

__FILE__

This will print full path to your file name in which this is used. If you have used this constant in included file then it will print the full path to the included file.

__DIR__

This will print full path to directory in which your file is places. Same as __FILE__, if you have used this in include then it will return full path to the included file’s directory.

This constant in introduced in PHP 5.3.

__FUNCTION__

You can use this constant in any function and it will return the name of the function in which that is used. In PHP 4 this constant return the function name always in lowercase but from the newer version of PHP 4 which is PHP 4.3.0 this constant will return function name as it is (case-sensitive).

__CLASS__

This will return the class name in which this is used. In PHP 4 it was returning always in lowercase but from PHP 5 it returns as it is (case-sensitive). PHP 5.4 this case be used in traits also. When used in a trait method, __CLASS__ is the name of the class the trait is used in.

__TRAIT__

This is introduced in PHP 5.4 and it will return the traits name in which this is used.

__METHOD__

This will return name of the class method name and it will return as it is.

__NAMESPACE__

This will return the name of the current namespace (case-sensitive). This constant is added in PHP 5.3.0.

Conclusion

This are the constants that can be useful based on need. But yes this are useful when you really need this values.

Previous:
Next:

Tagged with:

Written by

Avinash Zala currently working at a leading web development company as a Sr. PHP Web Developer. He is the founder of the Xpert Developer. He has completed B.E. in Information Technology. Born and brought up in Ahmedabad. He loves to code and coding is all about passion for him. Follow him on twitter at Avinash Zala & Xpert Developer.

View all articles by

Website: http://www.xpertdeveloper.com

Related Articles

PHP const Keyword Explained

PHP const Keyword Explained

With PHP we can define such variables whose values can not be changed after assigning. These varibles called . What is const? Constants are one type of variable which we can define for any class with keyword . Value of …Read More

PHP “Magic Quotes” Explained

PHP “Magic Quotes” Explained

First of let me say that Magic Quotes is deprected from the PHP 5.3 and will be removed completely from the PHP 6. But as a developer you might face a situation when you have to work on application which …Read More

PHP coding tips for Performance Improvement

PHP coding tips for Performance Improvement

This post covers most performance improvement tips related to PHP coding. While working with small website or project it’s ok to ignore these tips, but when you are dealing with large website or project which is going to maintained for …Read More

PHP clearstatcache() Explained

PHP clearstatcache() Explained

For the functions like , , etc PHP caches the result of this function for each file for faster performance if function called again. But in some cases you want to clear this cached information, for the task like getting …Read More

PHP String’s unusual behaviour

PHP String’s unusual behaviour

Hi Friends, A post after very long time. Actually busy with work. While working I found some unusual behavoir of the PHP string with ++ operator. So I think it would be good to share. Its a simple PHP question, …Read More

Feedback 2 Leave a comment

    Pingback: PHP Magic Constants Explained | PHP | Syngu

    Pingback: Predefined Interfaces in PHP | Expert PHP Developer

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>