String Resolution Operator to object

108 Views Asked by At

How is it possible to create an object from the following string:

$object = 'User::class';

Normaly you would remove the quotes but its an dynamic call for example

$value = 'User::CONST_NAME_HERE'
function test($value, $delimiter = '::')
{
   list($class, $constant) = explode($delimiter, $value);
   $class = sprintf('%s::class', $class); // not working
}

What i'm trying to reach is create a simple function where you could give the classname and the const to get its value.

1

There are 1 best solutions below

0
Stevan Pavlovic On

Reference: Get value of dynamically chosen class constant in PHP

PHP already has a function which does exactly the same.

constant('User::CONST_NAME_HERE')

I hope this could help you achieve your goal.