Answer the question
In order to leave comments, you need to log in
Namespacs and creating a class object from a string with its name?
Basically, I have a set of classes named ServiceXX, where XX is a changing string.
I use Yii2, I prescribe the namespace of the class through use.
namespace app\modules\tracking\classes;
use app\modules\tracking\classes\ServiceFI;
***
$serviceName = 'ServiceFI';
$service = new $serviceName;
$serviceName = 'app\modules\tracking\classes\ServiceFI';
$service = new $serviceName;
Answer the question
In order to leave comments, you need to log in
$serviceName = 'ServiceFI';
$model = 'app\\modules\\tracking\\classes\\' . $serviceName;
$service = new $model;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question