Contact!

Magic Constants in PHP. What they are and how to use them

Magic Constants in PHP. What they are and how to use them

There are 9 ‘Magic Constants’ that developers can use in PHP. Unlike PHP ‘Predefined Constants’, ‘Magic Constants’ are ambiguously named ‘constants’ which change based on where they are used within your script. Some of these are more useful than others, and I imagine that you will have already seen and used ‘__FILE__’ and ‘__DIR__’ on a regular basis.

Magic constants can be useful for many things, from debugging to file management. Here are some simple examples showing how to use each ‘Magic Constant’.

Note: All of these ‘Magic Constants’ get resolved at compile time instead of runtime.

__LINE__

This constant returns the currently executed line number within your script.

1. <?php
2. echo __LINE__; // Result = 2
3. echo __LINE__; // Result = 3

__FILE__

We can use the ‘__File__’ magic constant to return the full path of the file being executed.

echo __FILE__; // Result = "/full/path/of/this/file.php"

If we wanted to include the example above in a separate file, it would return the path of the included file and not the path of the file we include the file in.

include "include.php"; // Result = "/full/path/to/include.php"

__DIR__

This useful ‘Magic Constant’ will return the full directory path of the currently executed file.

Note: ‘__DIR__’ will not return a trailing slash.

echo __DIR__; // Result = "/full/directory/path/of/file"

Like ‘__FILE__’, if we use ‘__DIR__’ in an included file, it will still return the directory of the included file.

include "/some/path/to/include.php"; // Result = "/some/path/to"

__FUNCTION__

Returns the name of the function we are currently calling.

function helloFunction() {
    echo __FUNCTION__; 
}

helloFunction(); // Result = "helloFunction"

__CLASS__

Returns the name of the class that we are currently using.

class HelloClass {
    public function helloMethod() {
        echo __CLASS__; 
    }
}

$x = new HelloClass;
$x->helloMethod(); // Result = "HelloClass"

__TRAIT__

This will return the name of the trait that we are currently using. It will also prepend the trait with the current namespace as well.

namespace HelloNamespace;

trait helloTrait {
    function helloMethod() {
        echo __TRAIT__; 
    }
}

class HelloClass {
    use helloTrait;
}

$x = new HelloClass;
$x->helloMethod(); // Result = "HelloNamespace\helloTrait"

__Method__

This ‘Magic Constant’ returns the name of the method that we are currently calling, in relation to the containing class. This will return the name of the class followed by the name of the method.

class HelloClass {
    public function helloMethod() {
        echo __METHOD__; 
    }
}

$x = new HelloClass;
$x->helloMethod(); // Result = "HelloClass::helloMethod"

__NAMESPACE__

Returns the name of the current namespace in use.

namespace HelloNamespace;

echo __NAMESPACE__; //Result = "HelloNamespace"

ClassName::class

Because ‘ClassName::class’ returns the fully qualified class name, it is really handy when using namespaces.

namespace HelloNamespace;

class HelloClass {}

echo HelloClass::class; // Result = HelloNamespace\HelloClass

Join the discussion!

You might like:

Create, register and use shortcodes in WordPress

Create, register and use shortcodes in WordPress

by Gav 18/03/2023

Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.

How to use guard clauses in JavaScript

How to/why use guard clauses in JavaScript

by Gav 16/03/2023

Learn how to improve code readability and performance by using guard clauses in JavaScript. Discover their benefits and best practices.

Implements and Extends, Object Oriented TypeScript

Implements and Extends, Object Oriented TypeScript

by Gav 15/03/2023

Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.

Reading/Parsing and Writing YAML files in PHP, Symfony

Reading/Parsing and Writing YAML files, PHP Symfony

by Gav 14/03/2023

In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.

Measuring code execution performance in JavaScript

Measuring code execution performance in JavaScript

by Gav 13/03/2023

Measuring code execution performance is an important way to identify bottlenecks. Use these methods in JavaScript to help optimise your code.

Measuring script/code execution time in PHP using microtime

Measuring script/code execution time in PHP, microtime

by Gav 06/03/2023

Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.

Regenerate WordPress media image sizes, programmatically

Regenerate WordPress media image sizes, programmatically

by Gav 25/02/2021

Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.

Magic Constants in PHP. What they are and how to use them.

Magic Constants in PHP. What they are and how to use them

by Gav 15/02/2021

Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.

Detecting Keypress JavaScript

Detect single and multiple keypress events: JavaScript

by Gav 16/10/2019

Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!