I am currently learning about design patterns and I came across strategy pattern. I understood it, but now I want to know what would be the best solution to change strategy at runtime. Is it good solution to use ENV variables? Example:
interface Logger{
public function log();
}
class SomeOtherLogger implements Logger {
public function log(){
echo "Logging to something else";
}
}
class FileLogger implements Logger {
public function log(){
echo "Logging to file";
}
}
class Main{
public function doSomething(Loggers $logger){
$logger->log();
}
}
$main = new main();
$main->doSomething(new FileLogger);
One thing that came across my mind is to use environment variable, but I am not sure what is the best way to do it, this one doesn't seem right to me.
$loggingStrategy = env('LOGGING_TYPE') . 'Logger';
$main = new main();
$main->doSomething(new $loggingStrategy);