[英]Passing named parameters to a php function through call_user_func_array
當嘗試使用任意參數集在子 class 中調用 function 時,我遇到了以下問題:
class Base{
function callDerived($method,$params){
call_user_func_array(array($this,$method),$params);
}
}
class Derived extends Base{
function test($foo,$bar){
print "foo=$foo, bar=$bar\n";
}
}
$d = new Derived();
$d->callDerived('test',array('bar'=>'2','foo'=>1));
輸出:
foo=2, bar=1
哪個...不是我想要的 - 除了使用 func_get_args 的索引順序重新組合數組之外,還有其他方法可以實現這一點嗎? 是的,當然,我可以簡單地傳遞整個數組並在 function 中處理它......但這不是我想做的。
謝謝
No. PHP 不支持命名參數。 只考慮參數的順序。 您可能可以使用ReflectionClass將代碼本身拆開來檢查 function 參數名稱,但最終您仍然需要使用它來重新排序數組。
庫存 PHP class ReflectionMethod是您的朋友。
例子:
class MyClass {
function myFunc($param1, $param2, $param3='myDefault') {
print "test";
}
}
$refm = new ReflectionMethod('MyClass', 'myFunc');
foreach ($refm->getParameters() as $p)
print "$p\n";
結果:
Parameter #0 [ <required> $param1 ]
Parameter #1 [ <required> $param2 ]
Parameter #2 [ <optional> $param3 = 'myDefault' ]
至此你就知道了目標function的參數名稱。 有了這些信息,您可以修改您的方法“ callDerived ”,並且可以根據參數名稱將數組重新排序為call_user_func_array 。
Good news, I had the same concern (I was looking for named arguments in PHP, like Python does), and found this useful tool: https://github.com/PHP-DI/Invoker
這使用反射 API 從數組中提供帶有一些 arguments 的可調用文件,並且還使用可選的 arguments 默認值用於數組中未定義的其他參數。
$invoker = new Invoker\Invoker;
$result = $invoker->call(array($object, 'method'), array(
"strName" => "Lorem",
"strValue" => "ipsum",
"readOnly" => true,
"size" => 55,
));
玩得開心
您可以簡單地傳遞一個數組並提取:
function add($arr){
extract($arr, EXTR_REFS);
return $one+$two;
}
$one = 1;
$two = 2;
echo add(compact('one', 'two')); // 3
這將提取為引用,因此幾乎沒有開銷。
我使用位掩碼而不是 boolean 參數:
// Ingredients
define ('TOMATO', 0b0000001);
define ('CHEESE', 0b0000010);
define ('OREGANO', 0b0000100);
define ('MUSHROOMS', 0b0001000);
define ('SALAMI', 0b0010000);
define ('PEPERONI', 0b0100000);
define ('ONIONS', 0b1000000);
function pizza ($ingredients) {
$serving = 'Pizza with';
$serving .= ($ingredients&TOMATO)?' Tomato':'';
$serving .= ($ingredients&CHEESE)?' Cheese':'';
$serving .= ($ingredients&OREGANO)?' Oregano':'';
$serving .= ($ingredients&MUSHROOMS)?' Mushrooms':'';
$serving .= ($ingredients&SALAMI)?' Salami':'';
$serving .= ($ingredients&ONIONS)?' Onions':'';
return trim($serving)."\n" ;
}
// Now order your pizzas!
echo pizza(TOMATO | CHEESE | SALAMI);
echo pizza(ONIONS | TOMATO | MUSHROOMS | CHEESE); // "Params" are not positional
對於那些仍然可能偶然發現這個問題的人(就像我一樣),這是我的方法:
自PHP 5.6起,您可以使用...如此處所述:
在這種情況下,您可以使用以下內容:
class Base{
function callDerived($method,...$params){
call_user_func_array(array($this,$method),$params);
}
}
class Derived extends Base{
function test(...$params){
foreach ($params as $arr) {
extract($arr);
}
print "foo=$foo, bar=$bar\n";
}
}
$d = new Derived();
$d->callDerived('test',array('bar'=>'2'),array('foo'=>1));
//print: foo=1, bar=2
有一種方法可以做到這一點,並且正在使用 arrays (最簡單的方法):
class Test{
public $a = false;
private $b = false;
public $c = false;
public $d = false;
public $e = false;
public function _factory(){
$args = func_get_args();
$args = $args[0];
$this->a = array_key_exists("a",$args) ? $args["a"] : 0;
$this->b = array_key_exists("b",$args) ? $args["b"] : 0;
$this->c = array_key_exists("c",$args) ? $args["c"] : 0;
$this->d = array_key_exists("d",$args) ? $args["d"] : 0;
$this->e = array_key_exists("e",$args) ? $args["e"] : 0;
}
public function show(){
var_dump($this);
}
}
$test = new Test();
$args["c"]=999;
$test->_factory($args);
$test->show();
完整的解釋可以在我的博客中找到: http://www.tbogard.com/2013/03/07/passing-named-arguments-to-a-function-in-php/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.