Dynamic Class Constant Edge Cases

<?php

class X {
    const A = null;
}

// ?? works with class constants
echo X::A ?? 3;

// parse error, invalid syntax
//echo X::{A} ?? 3;

// quotes are needed, or a global constant
const A = 'A';
echo X::{A};

// @ does not hide undefined constants
echo @X::{'B'};

PHP 8.3 introduces a new syntax to access dynamically class constants: class::{constant-name}.

Class constants works with the ?? coalesce operator, as long as the class constant itself has the value null. On the other hand, the ?? does not work with the dynamic class constant, and yields a parse error.

The dynamic class constant syntax works with unquoted constant names, but then, it requires the related global constant to be existing, and holding the actual name of the constant.

Finally, the no scream operator @ does not continue execution in case of a non existing class constant: it masks the error message, and dies.

See Also

PHP Features

Last updated: 14 July 2026