公共变量函数问题

发布于 2024-12-23 13:40:49 字数 805 浏览 2 评论 0原文

我有一个函数,允许访问我以前从未见过的变量函数。

正常功能:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2024-12-30 13:40:49

您可以使用 call_user_func 调用此回调/闭包,而无需先保存到临时变量:

call_user_func($this->api, $arg1, $arg2);

这是一个完整的示例:

class Foo {
    public function __construct() {
        // this is likely what "api_client" is returning (a closure)
        $this->api = function ($arg1, $arg2) {
            print "I was called with $arg1 and $arg2";
        };
    }

    public function call_api($arg1, $arg2) {
        return call_user_func($this->api, $arg1, $arg2);
    }
}

$f = new Foo();
$f->call_api('foo', 'bar');

或者,使用您的示例:

public function somepage(){
    call_user_func($this->api, 'GET', '/admin/orders.json');
}

You can use use call_user_func to call this callback/closure without having the save off to a temp var first:

call_user_func($this->api, $arg1, $arg2);

Here's a complete example:

class Foo {
    public function __construct() {
        // this is likely what "api_client" is returning (a closure)
        $this->api = function ($arg1, $arg2) {
            print "I was called with $arg1 and $arg2";
        };
    }

    public function call_api($arg1, $arg2) {
        return call_user_func($this->api, $arg1, $arg2);
    }
}

$f = new Foo();
$f->call_api('foo', 'bar');

Or, to use your example:

public function somepage(){
    call_user_func($this->api, 'GET', '/admin/orders.json');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文