1

Digging in magento core code I found some interesting code: https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php#L93

protected function createObject($type, $args)
{
    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]);
        case 9:
            return new $type(
                $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7], $args[8]
            );
        case 10:
            return new $type(
                $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7], $args[8], $args[9]
            );
        case 11:
            return new $type(
                $args[0],
                $args[1],
                $args[2],
                $args[3],
                $args[4],
                $args[5],
                $args[6],
                $args[7],
                $args[8],
                $args[9],
                $args[10]
            );
        case 12:
            return new $type(
                $args[0],
                $args[1],
                $args[2],
                $args[3],
                $args[4],
                $args[5],
                $args[6],
                $args[7],
                $args[8],
                $args[9],
                $args[10],
                $args[11]
            );
        case 13:
            return new $type(
                $args[0],
                $args[1],
                $args[2],
                $args[3],
                $args[4],
                $args[5],
                $args[6],
                $args[7],
                $args[8],
                $args[9],
                $args[10],
                $args[11],
                $args[12]
            );
        case 14:
            return new $type(
                $args[0],
                $args[1],
                $args[2],
                $args[3],
                $args[4],
                $args[5],
                $args[6],
                $args[7],
                $args[8],
                $args[9],
                $args[10],
                $args[11],
                $args[12],
                $args[13]
            );
        case 15:
            return new $type(
                $args[0],
                $args[1],
                $args[2],
                $args[3],
                $args[4],
                $args[5],
                $args[6],
                $args[7],
                $args[8],
                $args[9],
                $args[10],
                $args[11],
                $args[12],
                $args[13],
                $args[14]
            );
        default:
            $reflection = new \ReflectionClass($type);
            return $reflection->newInstanceArgs($args);
    }
}

What purpose for such awful code? Why can't we use reflection for less then 15 args like in default section?

  • Maybe because returning new $type() gives a different result than new ReflectionClass($type)->newInstanceArgs()? I don't mean to know a thing about it, but from what I can read, it could be an explanation. – Julien Lachal Apr 17 '15 at 14:46
  • @Lindar. Please keep a civilized language in here. – Marius Apr 17 '15 at 18:07
3

because its faster if its explicit and above 15 seams to be rare, so in most cases it will be fast

Not the answer you're looking for? Browse other questions tagged or ask your own question.