M
M
Miku Hatsune2015-11-09 19:34:34
PHP
Miku Hatsune, 2015-11-09 19:34:34

Class name concatenation in PHP?

Hi all.
There was such a sports interest, I once read somewhere that in some version of PHP they added a feature in which you can concatenate the class name on the fly.
Only I can’t remember where it was ... And didn’t I dream about it;)
That is, so as not to write something like:

$prefix = 'My_';
$name = 'SomeClass';
$class_name = $prefix.$name;

$object = new $class_name();

And do not produce an extra class_name variable. At first, I remembered that the concatenation of the class name is carried out in curly braces:
$object = new {$prefix.$name};
But no:
Parse error: syntax error, unexpected '{'

You can only collect the names of class methods in this way.
In general, is there such a feature in PHP? To collect the class name directly when creating an object.
It's not that I need it, I just remembered ... And I can't calm down.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shagguboy, 2015-11-09
@shagguboy

$r = new ReflectionClass($classname);
$objInstance = $r->newInstanceArgs($variables);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question