Answer the question
In order to leave comments, you need to log in
How to create a previously unknown instance of a class?
Is there some correct principle for writing / building code for a situation where you need to create an instance of a class, but we don’t know in advance what kind of class we need.
For example:
There is a banner, the array stores targeting options ['geo','date','etc'] and there are classes that implement checks of these targetings.
Option
foreach($banner['target'] as $target){
$obj=new $target();
$obj->check();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question