PHP approach to Magic Constants

In general Magic constant is the sum of the  series of numbers that are in Magic Squares. But in programming also it is possible to find Magic Constants as well.

However in PHP also magic constants are popular, in general they are like global predefined constants. The interesting fact here is, those constants are available due to execution of a script or scripts based on the place they have been called. Those are very useful and magically help and make the developers life easier.

If you are from C++ then this is not a new concept to you.

Following are magic constants and I tried to give an example to show how those constants are used in the script.

All the magic constants starts with __ (double underscores) and ends with __ (double underscores).

__LINE__

This gives the current line number where this magic constant is.

<?PHP
//__LINE__ magic constant
echo "The line number is ".__LINE__." .";
?>

__LINE__ is more important in the DEBUG mode.

__FUNCTION__

This returns the name of the function where the __FUNCTION__ is put inside.

<?PHP
echo "Show me the function I use\n";
echo showMyName();
function showMyName(){
$functionaName=__FUNCTION__;
return $functionaName;
}
?>

__FILE__

This returns the file name of the file name including the full path.

<?PHP
echo "The file path :  ".__FILE__;
?>

Above are the important magic constants that can be used frequently in your code. If you are into creative thinking you can find various ways to get the real benefit of those.

Advertisements

Please add your valuable idea below, will make a discussion, thanks !

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s