The following PHP code is perfectly valid and runs without an error:
<?php
class MyClass {
const CONST_VALUE = 'A constant value';
}
$classname = 'MyClass';
$identifier = 'CONST_VALUE';
echo $classname::{$identifier}, "\n";
The following PHP code is perfectly valid and runs without an error:
<?php
class MyClass {
const CONST_VALUE = 'A constant value';
}
$classname = 'MyClass';
$identifier = 'CONST_VALUE';
echo $classname::{$identifier}, "\n";
In PHP, the curly braces syntax ({}) you’re using—$classname::{$identifier}—is a valid and flexible way to dynamically access a class constant when the constant’s name is stored in a variable. This is particularly useful when the constant name isn’t known until runtime or is determined dynamically. Let me explain how it works and why it’s valid…
<?php
class MyClass {
const CONST_VALUE = 'A constant value';
}
$classname = 'MyClass';
$identifier = 'CONST_VALUE';
echo $classname::{$identifier}, "\n";