Question in the context of PHP 5.5+ and 7
There is a script, a kind of orchestrator, which, depending on the input parameters, instantiates different classes.
I know three fundamentally different approaches for implementation.
The first:
switch ($method) { case 'login': $call = new Login($data); break; case 'list': $call = new List($data); break; default: $call = new WrongMethod($data); } Second:
$methods = [ 'login' => 'Login', 'list' => 'GetList', ]; $className = isset($methods[ $method ]) ? $methods[ $method ] : 'WrongMethod'; $call = new $className($data); Third (proposed by Dmitriy Simushev):
$methods = [ 'login' => Login::class, 'list' => GetList::class, ]; $className = isset($methods[ $method ]) ? $methods[ $method ] : WrongMethod::class; $call = new $className($data); What can you say for the disposal of memory and the speed of each of them? (I suspect that by memory will win the second, but I can not prove)