php入門到就業線上直播課:進入學習
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調試工具:點擊使用
年前花了點時間,對Yar的性能做了一些做了一些提升,但是也遇到一個讓我有點不舒服的當初沒有良好設計遺留的問題,就是在并行調用RPC的時候,現在的方法原型是:
public static Yar_Concurrent_Client::call(string $uri, string $method, ?array $arguments = NULL, ?callable $callback = NULL, ?callable $error_callback = NULL, ?array $options = NULL):null|int|bool {}
是不是一看就很頭大?
因為在實際的使用過程中,很有可能回調函數和錯誤回調函數是空的,因為可以真正發起調用的時候,也就是在loop重全局指定:
Yar_Concurrent_Client::loop(?callable $callback = NULL, ?callable $error_callback = NULL, ?array $options = NULL):?bool {}
而很多時候$options是有用的,根據調用相關,所以就導致,實際的使用的時候,大量的并行調用的代碼會在參數中寫很多的NULL, 類似:
Yar_Concurrent_Clinet::call("https://xxx.com/api", "method", array("arguments"), NULL, NULL, array(YAR_OPT_HEADER=>array("header:val1")); Yar_Concurrent_Clinet::call("https://xxx.com/api", "method", array("arguments"), NULL, NULL, array(YAR_OPT_HEADER=>array("header:val2")); Yar_Concurrent_Clinet::call("https://xxx.com/api", "method", array("arguments"), NULL, NULL, array(YAR_OPT_HEADER=>array("header:val2")); Yar_Concurrent_Clinet::call("https://xxx.com/api", "method", array("arguments"), NULL, NULL, array(YAR_OPT_HEADER=>array("header:val4"));
于是我一直想如何能讓這樣的調用更優雅一些,曾經一度我想使用多態,或者新增一個API,類似:
public static Yar_Concurrent_Client::callArray(array $arguments):null|int|bool {}
但強迫癥讓我覺得這樣做,遺禍無窮, 今天早上我突然想起以前曾經看到過的一個RFC,于是找了半天,發現早在PHP5.6的時候,就commit了, 但反正我比較老派,新特性研究的少,也是沒怎么用過,就不知道大家是否會用過了。 ?
就是今天要介紹的第一個特性:Argument unpacking。
Argument unpacking
我們知道PHP支持可變參數,也就是variadic function. 比如對于如下的函數定義:
function variadic(...$arguments) { var_dump($arguments); }
注意參數的定義,使用了三個點…(ellipsis符號), 意思就是無論你在調用這個函數的時候傳遞了多少個參數,這些參數都會被打包成一個名字為$arguments的數組:
variadic(); //output: array(0) { } variadic(NULL); //output: array(1) { [0]=> NULL } variadic("foo", "bar"); //output: array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } variadic(NULL, array(), "dummy"); //output: array(3) { [0]=> NULL [1]=>[] [2]=> string(5) "dummy" }
當然,這個不是我們今天要用到的,這個特性還有一個對應的在調用時刻使用的兄弟形式,叫做argument unpacking:
比如,類似上面我的那個問題,我們定義了一個函數
function dummy($a, $b = NULL, $c = NULL, $d = NULL, $e = NULL) { var_dump($a, $b, $c, $d, $e); }
如果大部分情況下我們的參數b, c, d都是NULL, 但是e可能需要傳遞,那我們就可以使用argument unpacking來避免代碼中大量的NULL參數,類似:
$arguments = array( "First argument", NULL, NULL, NULL, "Fifth argument", ); dummy(...$arguments); //output: // string(14) "First argument" // NULL // NULL // NULL // string(14) "Fifth argument"
注意在調用的時候,我也使用了…,這里的意思就是,把…后面的數組解開,按照順序分別依次傳遞給被調用的函數,第一個元素對應第一個參數, 第二個對應第二個。
但是注意,這里的位置是跟填充位置相關的,跟索引無關,也就是說:
$arguments = array( 4=> "First argument", 0=> "Fifth argument" ),
這樣的形式, 索引4依然是被認為是第一個參數。
想到這個以后,我就突然發現,我不需要給Yar引入新東西了,最開的例子就可以變成:
$arguments = array( "https://xxx.com/api", "method", array("arguments"), NULL, NULL, "options" => array(YAR_OPT_HEADER => array("header:val1") ) Yar_Concurrent_Clinet::call(...$arguments); $arguments["options"][YAR_OPT_HADER] = ["header:val2"]; Yar_Concurrent_Clinet::call(...$arguments); $arguments["options"][YAR_OPT_HADER] = ["header:val3"]; Yar_Concurrent_Clinet::call(...$arguments); $arguments["options"][YAR_OPT_HADER] = ["header:val4"]; Yar_Concurrent_Clinet::call(...$arguments); Yar_Concurrent_Clinet::call(...$arguments);
你以為這就完了么?
考慮到如上的代碼,還是有一個問題,就是需要構造一個中間數組,對于強迫癥的我們來說,總還是覺得會有點,那啥…
但其實我們還可以利用PHP8.0中引入的另外一個RFC, Named parameter:
Named Parameter
在PHP8.0以后,容許用戶在傳遞參數的時候,指定參數名字, 比如還是對于上面的例子函數:
function dummy($a, $b = NULL, $c = NULL, $d = NULL, $e = NULL) { var_dump($a, $b, $c, $d, $e); }
現在我們可以在調用的時候,指定要傳遞的參數名字,比如:
dummy(a:"dummy", e:"foo"); //output: // string(5) "dummy" // NULL // NULL // NULL // string(3) "foo"
也就是說,我指定了傳遞給a和e參數,沒有指定的就是默認缺省值,你甚至可以不按聲明順序來,比如:
dummy(e:"foo", a:"dummy");
輸出結果也是一樣的。
這樣以來,開頭的代碼就可以變成:
Yar_Concurrent_Client::call("https://xxx.com/api", "method", arguments:array("arguments"), options:array(YAR_OPT_HEADER=>array("header:val1"))); Yar_Concurrent_Client::call("https://xxx.com/api", "method", arguments:array("arguments"), options:array(YAR_OPT_HEADER=>array("header:val2"))); Yar_Concurrent_Client::call("https://xxx.com/api", "method", arguments:array("arguments"), options:array(YAR_OPT_HEADER=>array("header:val3"))); Yar_Concurrent_Client::call("https://xxx.com/api", "method", arguments:array("arguments"), options:array(YAR_OPT_HEADER=>array("header:val4")));
你就可以在調用的時候,想傳那個傳那個,想怎么傳就怎么傳
雖然代碼相比argument unpacking還是有點長,但是既解決了不用寫那么多NULL,又不會引入新的中間變量。
于是,問題完美解決,我也不用引入新的API了 :)
原文地址:https://www.laruence.com/2022/05/10/6192.html
推薦學習:《PHP視頻教程》