PHP’s Magic Constants: __LINE__ , __FILE__, __FUNCTION__, __CLASS__, and __METHOD__
Setting constants in PHP is as easy as using the define function, but PHP creates a few constants in every script for you that help mostly for debugging purposes (well, that's generally the only time I use them). These constants are called "magic constants."
Magic constants have a funny syntax, placing two underscores before and after the constant's word representation. These are PHP's magic constants:
__LINE__
__FILE__
__FUNCTION__
__CLASS__
__METHOD__
You can imagine how helpful these constants are for debugging...but if you can't:
if($sugar == '') { echo('$sugar has no value on line '.__LINE__.' of ['.__FILE__.']'); }
There are also helpful uses for these magic constants:
dirname(__FILE__) // get the directory name of the current script
What do you use them for?
![9 More Mind-Blowing WebGL Demos]()
With Firefox OS, asm.js, and the push for browser performance improvements, canvas and WebGL technologies are opening a world of possibilities. I featured 9 Mind-Blowing Canvas Demos and then took it up a level with 9 Mind-Blowing WebGL Demos, but I want to outdo...
![CSS 3D Folding Animation]()
Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...
![Multiple File Upload Input]()
More often than not, I find myself wanting to upload more than one file at a time. Having to use multiple "file" INPUT elements is annoying, slow, and inefficient. And if I hate them, I can't imagine how annoyed my users would be. Luckily Safari, Chrome...
![Web Audio API]()
The Web Audio API allows developers to load and decode audio on demand using JavaScript. The more I evaluate awesome games for Firefox OS TVs, the more I get to learn about these APIs that I normally wouldn't touch. The following is a very basic introduction to the WebAudio API...
My two favorite are
That you can use it to get parent directory:
or you can include the init.php file from anywhere in your filesystem:
You would use
in the init.php
Wow, awesome usage of the __FILE__ magic constant — I hadn’t thought of that one!
Useful!
To Jesus DeLaTorre:
Maybe it could be better, when you use this:
$dir = str_replace ( “\”, “/”, dirname ( _FILE_ ) )
-> for Windows based systems
(PS: nice book engine)
thanks for this, i knew only __FILE__ so i googled this article :)
@jeezaa:
Instead of
/
for linux and\
for windows, just use another constant:omg @ the comments
realpath(dirname(__FILE__)) or
__DIR__ or
realpath(dirname(__FILE__).’/..’).’/file.bla’
is there any way to get the constants defined in a php file using
define()?
Yes there is. The function get_defined_constants() shows you all of the defined constants see: http://www.php.net/manual/en/function.get-defined-constants.php.
I have never seen the use of __FUNCTION__
Can give me any Example.
I make it a habit to wrap these magic constants into SQL and PL/SQL blocks so that I can quickly trace the construction of queries and DML to the source.
There’s also the
__NAMESPACE__
constant, e.g.