cancel
Showing results for 
Search instead for 
Did you mean: 

Use splat operator from PHP 5.6 in...

0 Kudos

Use splat operator from PHP 5.6 in...

Feature request from airbone42, posted on GitHub Feb 16, 2015

Hi,

I just stumbled over the createObject function which has a massive switch to decide how an object should be created, depending on the amount of arguments.

    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);
    }

So PHP 5.6 added a new operator, also named the splat operator in other languages to unpack arguments, so whenever you decide to change the min. version to 5.6 this massive switch could be replaced by this simple line:

return new $type(...$args);
(1)
4 Comments
apiuser
New Member

Comment from orlangur, posted on GitHub Feb 16, 2015

Thanks, this well-known new feature was planned to be tried where applicable if performance boost is significant.

Also, even if PHP 5.6 will not be a minimum supported version we can switch ObjectManager implementations using DI or place such in separate composer package with appropriate PHP requirement.

apiuser
New Member

Comment from antonkril, posted on GitHub Feb 16, 2015

Interception will also benefit from this feature.

apiuser
New Member

Comment from aohorodnyk, posted on GitHub Jun 25, 2015

Performance test Splat new faster than createObject method ~21% But createObject faster than reflection only ~6%

In default installation magento2 has 700+ shared objects and 2100+ used createObject method, argument resolving slowly than creating new object, I think that using any method will not have important performance improvement or performance degradation for magento2 project.

apiuser
New Member

Comment from adragus-inviqa, posted on GitHub Jun 13, 2016

Should this be closed now?