0

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.

b.dappr
  • 11
  • 2
  • Seems to work, maybe make a return statement: return sprintf('%s::class', $class); and call it... – ka_lin Oct 04 '19 at 14:36

1 Answers1

0

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.