I tried the code below:
$dyn = "new ". $className . "(" .$param1 . ", ". $param2 . ");"; $obj = eval($dyn);
It compiles, but is null.
How can you instantiate an object in PHP dynamically?
$class = 'ClassName'; $obj = new $class($arg1, $arg2);
If you really want to use eval- what chances you should not, if you are new to PHP;) - you would do something more ...
eval
$dyn = "new ". $className . "(" .$param1 . ", ". $param2 . ");"; eval("\$obj = $dyn");
? eval , , , .
, factory , , , ,
Source: https://habr.com/ru/post/1728792/More articles:When should I create a new controller class in Asp.net MVC (design question)? - design-patternsWhat does an expression mean for compiler assembly? - c ++What happens when I add a Django application to INSTALLED_APPS? - pythonDynamic injection of bean properties based on locale variant in Spring - javaПолучение Spring IOC для работы с шаблоном MVP - javahttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1728793/outputting-all-array-values-to-file-with-php&usg=ALkJrhjuQk4hfenSAS2hV2caOUu9ts-xOgDefinition of the complexity of time and space - big-odownloading the compiled program to the C51 microcontroller - embeddedКак использовать предложение mySQL WHERE в PHP? - phpHow does PHP handle variables in RAM? - phpAll Articles