Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Variables and Constants

PHP magic constants

Reference for PHP magic constants: __LINE__, __FILE__, __DIR__, __FUNCTION__, __CLASS__, __METHOD__, __NAMESPACE__, __TRAIT__ and ClassName::class, with examples.

Magic constants look like constants but their value depends on where they are written. PHP replaces them at compile time with information about the current line, file, function, class or namespace. There are eight of them, always written with two underscores on each side, case-insensitive (but conventionally upper case), and they cannot be assigned or redefined.

They are useful for logging, error messages, building file paths relative to the current file, and for writing code that does not repeat its own name. Closely related, and often grouped with them, is ::class, which gives the fully qualified name of a class without creating an object.

PHP
<?php
echo __LINE__;                    // the line number of this statement
require __DIR__ . "/helpers.php"; // path relative to this file

Try it

PHP Line, function, class and method

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP File and directory, closures and traits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

The constants

Constant Value
__LINE__ The current line number in the file.
__FILE__ Full path and file name of the current file, with symbolic links resolved. In an included file it is the included file.
__DIR__ Directory of the current file; same as dirname(__FILE__), without a trailing slash (except for the root directory).
__FUNCTION__ The name of the current function, a name such as {closure:main.php:7} for a closure (older versions print just ), empty outside functions.
__CLASS__ The name of the class where the code is written, including its namespace. In a trait: the class that uses it.
__TRAIT__ The name of the trait, including namespace; empty outside a trait.
__METHOD__ Class::method inside a method; the function name inside a function.
__NAMESPACE__ The current namespace, "" in the global namespace.
ClassName::class The fully qualified class name as a string, resolved at compile time. Not strictly a magic constant, but used the same way.

Usage notes

  • __CLASS__ is where it was written; static::class is the object's real class. In the example, __CLASS__ stays Logger for a FileLogger object, while static::class is FileLogger.
  • ::class does not load the class. Foo::class is just the string "Foo" (qualified through use and namespace rules). It works for classes that do not exist. On an object, $obj::class returns its class name.
  • Compile-time values. __LINE__, __FILE__ and __DIR__ are fixed when the file is compiled. A file that is included from elsewhere still reports its own path, which makes require __DIR__ . "/x.php" reliable regardless of the working directory.
  • __DIR__ beats relative paths. include "lib/a.php" depends on the current working directory and the include path; __DIR__ . "/lib/a.php" does not.
  • Closures get a generated name. Inside a closure __FUNCTION__ is in older PHP versions and includes the file and line in newer ones (the example prints {closure:main.php:7}). Do not parse it. If you need the surrounding function, use debug_backtrace() or pass the name in.
  • Not usable inside strings. "Line __LINE__" is plain text. Concatenate: "Line " . __LINE__.
  • Use them in log messages. error_log(__METHOD__ . ": failed") finds the origin quickly, and renaming the class keeps it correct.
  • Rely on names, not paths, in output. __FILE__ shows the server's directory layout, which you should not reveal to visitors.
  • Not ordinary constants. They are resolved by the compiler, so you cannot redefine them or use them as the name of a const.