PHP’s Magic Constants: __LINE__ , __FILE__, __FUNCTION__, __CLASS__, and __METHOD__

By  on  

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?

Recent Features

  • By
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

Incredible Demos

  • By
    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...

  • By
    Implement jQuery’s hover() Method in MooTools

    jQuery offers a quick event shortcut method called hover that accepts two functions that represent mouseover and mouseout actions. Here's how to implement that for MooTools Elements. The MooTools JavaScript We implement hover() which accepts to functions; one will be called on mouseenter and the other...

Discussion

  1. My two favorite are

    That you can use it to get parent directory:

    dirname(dirname(__FILE__))
    

    or you can include the init.php file from anywhere in your filesystem:

    You would use

    include_once(dirname(__FILE__) . '/database.class.php');
    

    in the init.php

  2. Wow, awesome usage of the __FILE__ magic constant — I hadn’t thought of that one!

  3. Jaime

    Useful!

  4. To Jesus DeLaTorre:

    “dirname(dirname(__FILE__)”

    Maybe it could be better, when you use this:

    $dir = str_replace ( “\”, “/”, dirname ( _FILE_ ) )
    -> for Windows based systems

    (PS: nice book engine)

  5. thanks for this, i knew only __FILE__ so i googled this article :)

  6. Jim

    @jeezaa:

    Instead of / for linux and \ for windows, just use another constant:

    include_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'database.class.php);
  7. Petah

    omg @ the comments

    realpath(dirname(__FILE__)) or

    __DIR__ or

    realpath(dirname(__FILE__).’/..’).’/file.bla’

  8. is there any way to get the constants defined in a php file using
    define()?

  9. I have never seen the use of __FUNCTION__
    Can give me any Example.

  10. Allan Peda

    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.

  11. There’s also the __NAMESPACE__ constant, e.g.

    namespace hyperdrive;
    add_action('wp_head', __NAMESPACE__ .'\engage');
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!