Skip to content
Advertisement

Const access from outside a class contained in string

With this code:

<?php

class Constants{
   const ONE = 1;
   const TWO = 2;
   const THREE = 3;
}

$input = "ONE";

echo Constants::$input;

?>

I want to access to the constants inside the class having the name in a variable.

I’ts that posible.

Advertisement

Answer

constant function will return value of a constant by its name:

class Contants{
   const ONE = 1;
   const TWO = 2;
   const THREE = 3;
}

$input = "ONE";

echo constant(Contants::class . '::' . $input);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement