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

Dynamic Class Constant

To access a constant value with its name in a string, one required the constant() function. constant('\A::'.$constantName).

In PHP 8.3, there is a dedicated syntax, to access those constants dynamically.

PHP code

<?php

class a {
	public const A = 1;
}

$b = 'A';

echo A::{$b};

?>

Before

PHP Parse error:  syntax error

After

1

PHP version change

This behavior changed in 8.3.

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat