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

Special Constant

The only special constant is the ::class constant. It has the same syntax as a class constant, hence the name. Yet, it is always defined, and holds the fully qualified name of the related class, interface, trait or enumeration.

It is convenient for class name resolution, in particular with namespaced classes.

The resulting name does not include the initial \.

::class is similar to the get_class() function, although it is compatible with static, self or parent.

The notion of special constant is distinct from the magic constants, such as __FILE__.

<?php

    use Herd\Waterhole\Elephpant as Trunk;

    echo Herd::class;  // Herd

    echo Trunk::class;  // Herd\Waterhole\Elephpant

    class Calf {
        function trumpet() {
            echo static::class;  // Calf
        }
    }
    
    echo $object::class; // similar to get_class($object)

?>

Documentation

See Also