問題描述
我有一個為 WSDL 文件生成的 SoapClient 實例.除了其中一個方法調用之外的所有方法都需要傳遞用戶名和密碼 id.
I have a SoapClient instance generated for a WSDL file. All except one of the method invocations require the username and the password to be passed id.
有什么辦法可以將方法調用柯里化,這樣我就可以省略用戶名和密碼嗎?
Is there any way of currying the method calls so that I can omit the username and password?
推薦答案
從 php 5.3 開始,您可以存儲 變量中的匿名函數.這個匿名函數可以使用一些預定義的參數調用原始"函數.
As of php 5.3 you can store an anonymous function in a variable. This anonymous function can call the "original" function with some predefined parameters.
function foo($x, $y, $z) {
echo "$x - $y - $z";
}
$bar = function($z) {
foo('A', 'B', $z);
};
$bar('C');
您還可以使用閉包來參數化匿名函數的創建
edit: You can also use a closure to parametrise the creation of the anonymous function
function foo($x, $y, $z) {
echo "$x - $y - $z";
}
function fnFoo($x, $y) {
return function($z) use($x,$y) {
foo($x, $y, $z);
};
}
$bar = fnFoo('A', 'B');
$bar('C');
edit2:這也適用于對象
edit2: This also works with objects
class Foo {
public function bar($x, $y, $z) {
echo "$x - $y - $z";
}
}
function fnFoobar($obj, $x, $z) {
return function ($y) use ($obj,$x,$z) {
$obj->bar($x, $y, $z);
};
}
$foo = new Foo;
$bar = fnFoobar($foo, 'A', 'C');
$bar('B');
但是如果您想增強"一個完整的類,使用 __call() 和包裝類的其他建議可能會更好.
But the other suggestions using __call() and a wrapper class may be better if you want to "enhance" a complete class.
這篇關于是否可以在 PHP 中咖喱方法調用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!