Search code examples
phparraysfunctioncall

Declare Array Inside Function Call PHP


Hey just wondering if there is a simpler way to declare an array inside a function call besides array()

$setup = new setupPage();
$setup->setup(array(
                   type => "static",
                   size => 350
                 ));

class setupPage {
    public function setup($config){
        echo $config[size] . $config[type];
    }
}

Thanks :D


Solution

  • If you use PHP 5.4+ you can use the shorthand, however it makes no difference in performance, but in actuality may make it harder to read:

    $setup->setup(['type' => 'static',
                   'size' => 350]);