Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Magic Constants

Constants which hold information about the current location of the code.

Those constants are magic, because they have a constant syntax, yet their value may change, even during the execution of the code.

  • LINE The current line number of the file.
  • FILE The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
  • DIR The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
  • FUNCTION The function name, or {closure} for anonymous functions.
  • CLASS The class name. The class name includes the namespace it was declared in, e.g. Foo\Bar. When used in a trait method, CLASS is the name of the class the trait is used in.
  • TRAIT The trait name. The trait name includes the namespace it was declared in, e.g. Foo\Bar.
  • METHOD The class method name.
  • NAMESPACE The name of the current namespace.
  • PROPERTY The name of the current property hook.

Those constants are case insensitive. While the common convention is to use them all in uppercase, all the following syntaxes work: __METHOD__, __method__, __meTHod__.

<?php

function trumpet() {
    // shows trumpet
    echo __FUNCTION__;
}

function forage() {
    // shows forage
    echo __FUNCTION__;
}

?>

Documentation

See Also