Sticky Posts
Jan 17, 2009
Magic PHP Constants: FILE, LINE, FUNCTION, CLASS, METHOD … Do Magic
These magic constants are not as well known as they should be, neither as appreciated as they should be. These constants do magic if one knows how to use them. But, to know how to use them, one has to actually understand them. I'll try in this post to explain them to you and somehow change the way you code and distribute your files. So let's try to understand each of them.
How these constants work?
Mandatory for you to understand is that each of these constants is relative to its location in the source code. Each depends on the very thing's name it wears. So at least one of them will change if the line number they are used on changes.These are constants and can't be assigned a new value. They are readonly yet change all the time.
__LINE__
Is the most volatile of them all and changes for each line.
__FILE__
This is the current file path. It's the complete path of the file you are currently editing. This is not relative to DOCUMENT_ROOT or any of these server variables.
__FUNCTION__
This is the current function name. This can do a lot of magic and tells if the current line of code is inside a function body or free. Free code means code in the global scope, not included inside a function or a class. It's equivalent of __METHOD__ but will not include the __CLASS__:: in the output;
__CLASS__
This is the current class and will output the name of the class only if used between {} of a class.
__METHOD__
This is same as __FUNCTION__, but, if called inside a class it will output the equivalent of __CLASS__::__FUNCTION__.
Why are these so important?
These are the tools of custom debugging and can help the coder to some things that one would not be able to do without them. __FILE__ allows you to write code independent of folder location. Your files can be moved around and you code would still work if you harness the power of __FILE__ PHP Magic constant.
Let's say your script depends on a text file provided along with it. And the text file is located in the same folder with your script. If you need to read that file's contents you will usually use file_get_contents and provide the relative or absolute path of the file. Absolute paths won't allow moving and relative paths depend on current directory (getcwd) which can be easily changed (chdir) and is never reliable, but dirname(__FILE__) will always return the path of the current file allowing you to always be able to access dependencies easily.
__FUNCTION__ allows you to check if current code runs inside a function. Let's say you provide a PHP filebut you don't want people to include it in functions. You can check strlen(__FUNCTION__)>0 and you can die($why); . Cool stuff!
A beginner mistake!
A beginner may imagine he has hit the Holly Grail of debugging and write the following naive debugging function.
<?php
function functional_debug($params){
print_r($params);
}
// ... and somewhere down the road
functional_debug(array('FILE' => __FILE__,'LINE' => __LINE__,'FUNCTION' => __FUNCTION__,'CLASS' => __CLASS__,'METHOD' => __METHOD__));
?>
But this is wrong in so many ways as the function will always output the same thing. As I said before these constants are hardlinked to the place where they are called. These need to be fed as parameters to a function if one needs them handled elsewhere. Like this:
<?php
function naive_debug(){
print_r(
array(
'FILE' => __FILE__,
'LINE' => __LINE__,
'FUNCTION' => __FUNCTION__,
'CLASS' => __CLASS__,
'METHOD' => __METHOD__
)
);
}
// ... and somewhere down the road call it
naive_debug();
?>
This works as expected. These constants are fed as an array to a function and they will retain expected values. Function can further process / record the parameters. Debugging 101.
Nothing beats a good example!
This is another example that, if you did not understand anything from all I wrote in this post, will clear you out by showing you how it actually works. Create a new PHP file on your server and paste these. Then run it. Look at the code and line numbers and compare against the output. If you still don't get it after this example ... slap me in the comments form.
Registration is quick, painless and worth its weight in gold.
Play with the example's code to do your own tests.
I'll discuss debugging more in depth further down the road. There are dedicated functions in PHP that do even more then you can achieve by this.

