In magento 2 the classes are instantiated using an object manager factory: \Magento\Framework\ObjectManager\Factory\Factory::create()
.
So far so goo. But there is something that I don't understand here.
After checking if there are no circular references in the arguments that should be passed to the constructor for DI and after the arguments are determined there is this ($args
are the arguments that should be passed to the constructor):
switch (count($args)) {
case 1:
return new $type($args[0]);
case 2:
return new $type($args[0], $args[1]);
case 3:
return new $type($args[0], $args[1], $args[2]);
case 4:
return new $type($args[0], $args[1], $args[2], $args[3]);
case 5:
return new $type($args[0], $args[1], $args[2], $args[3], $args[4]);
case 6:
return new $type($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
case 7:
return new $type($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6]);
case 8:
return new $type($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7]);
default:
$reflection = new \ReflectionClass($type);
return $reflection->newInstanceArgs($args);
}
Why is this long switch
statement? Why not use directly the code from the default
branch?
Or why stop the case
at 8? Why not 5 or 10 or 127?