PHP ... operator 使用方式

今天發現一個很酷的 php 5.6 以後的 feature

直接 show code 比較快

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$string = "Controller@home";
test(...explode("@", $string));
function test($controller_name, $funciton_name)
{
var_dump($controller_name, $funciton_name);
}
// print result
// string(10) "Controller"
// string(4) "home"

這個 operator 會直接將 explode 出來的 array 直接依序的放入 function 中