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: