公共变量函数问题
我有一个函数,允许访问我以前从未见过的变量函数。
正常功能:
$api = api_client($special_data);
$data = $api('get','something.json'); // notice $api() not a mistake
上面示例的问题是我在控制器的每个函数/方法中创建 $api 变量。我想做这样的事情:
public $api;
public function somepage(){
$special_data = get_special_data_from_this_method();
$this->api = api_client($special_data);
}
public function anotherpage(){
$data = $this->api('get','something.json'); // api is not a function it is a variable function
}
我确实发现以下作品,虽然我不满意它
public function somepage(){
$special_data = get_special_data_from_this_method();
$this->api = api_client($special_data);
$temp = $this->api;
$data = $temp('GET', '/admin/orders.json');
}
希望这是有道理的会喜欢帮助!
I have a function that permits access to something I've never seen before a variable function.
normal functionality:
$api = api_client($special_data);
$data = $api('get','something.json'); // notice $api() not a mistake
The problem with this above example is that I am createing the $api variable in each function / method of my controller. I would like to do something like this:
public $api;
public function somepage(){
$special_data = get_special_data_from_this_method();
$this->api = api_client($special_data);
}
public function anotherpage(){
$data = $this->api('get','something.json'); // api is not a function it is a variable function
}
I did find that the following works, although I am not satisfied with it
public function somepage(){
$special_data = get_special_data_from_this_method();
$this->api = api_client($special_data);
$temp = $this->api;
$data = $temp('GET', '/admin/orders.json');
}
Hope this makes sense would love the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
call_user_func
调用此回调/闭包,而无需先保存到临时变量:这是一个完整的示例:
或者,使用您的示例:
You can use use
call_user_func
to call this callback/closure without having the save off to a temp var first:Here's a complete example:
Or, to use your example: