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

Class Constant Syntax

The class constant syntax is the full name of a class constant, formalized in a string. It is namespace\class-name::constant-name.

That representation requires the name of the class to include the full namespace path, with or without the leading \.

The class constant syntax does not work with local use expressions.

The class constant syntax works with the constant() and defined() functions.

<?php

    namespace Zoo\Habitat;

    use Elephpant as Trunk;

    class Elephpant {
        const HABITAT = 'Savanna';
    }

    echo constant('\Zoo\Habitat\Elephpant::HABITAT');
    echo constant('Zoo\Habitat\Elephpant::HABITAT');
    echo constant('Trunk::HABITAT'); // error, no such class as Trunk

?>

Documentation

See Also