阅读“ this”和“使用” PHP闭包的参数 [英] Reading "this" and "use" arguments from a PHP closure

查看:139
本文介绍了阅读“ this”和“使用” PHP闭包的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建在PHP中返回闭包的方法时:

  class ExampleClass {
公共函数test( ){
$ example = 10;

return function()use($ example){
return $ example;
};
}
}

print_r 包含 this (其方法创建闭包的类)和 static 闭包的 use()语句中绑定的值:

  $ instance = new ExampleClass(); 
$ closure = $ instance-> test();

print_r($ closure);

生产:

 关闭对象(
[静态] =>数组(
[example] => 10

[this] =>例ExampleClass Object()

但是我一生无法解决如何捕捉这些价值。不可能使用任何形式的属性访问器(例如 $ closure-> static $ closure-> {'static'} ),而不会收到以下内容:


PHP致命错误:未捕获错误:Closure对象在XYZ中不能具有属性。 / p>

数组访问符号显然也不起作用:


PHP致命错误:未捕获错误:无法将Closure类型的对象用作XYZ中的数组。


JSON编码对象,此外这使得值在它们为对象的情况下变得无用,提供了一个空的JSON对象 {} ,并且使用 ReflectionFunction 类不提供



关闭文档也不提供任何访问这些值的方法。



除了做一些可耻的事情之外像输出缓冲和解析 print_r 或类似方法,我实际上看不到获得这些值的方法。



我缺少明显的东西吗?


注意:用例用于实现备忘录,这些值将非常有益



解决方案

已经忽略了某些 ReflectionFunction 方法。



看看 ReflectionFunction :: getClosureThis() 方法。我通过搜索 zend_get_closure_this_ptr() ,它在 zend_closures.c



该手册目前没有有关此功能的大量文档。我正在使用7.0.9;尝试根据您的示例运行以下代码:

  class ExampleClass {
private $ testProperty = 33;

公共功能test(){
$ example = 10;

return function()use($ example){
return $ example;
};
}
}

$ instance = new ExampleClass();
$ closure = $ instance-> test();

print_r($ closure);

$ func = new ReflectionFunction($ closure);
print_r($ func-> getClosureThis());

您应该获得类似于



<$ p的输出$ p> 关闭对象

[静态] =>数组

[示例] => 10


[this] => ExampleClass对象

[testProperty:ExampleClass:private] => 33




ExampleClass对象

[testProperty:ExampleClass:private] => 33

关于闭包静态变量,这些变量通过 ReflectionFunction :: getStaticVariables()


返回

  php> var_dump($ func-> getStaticVariables()); 
array(1){
[ example] =>
int(10)
}


When you create a method that returns a closure in PHP:

class ExampleClass {
  public function test() {
    $example = 10;

    return function() use ($example) {
      return $example;
    };
  }
}

The result of print_r contains this (the class whose method created the closure) and static, which appears to be the values bound within the use () statement of the closure:

$instance = new ExampleClass();
$closure = $instance->test();

print_r($closure);

Producing:

Closure Object (
    [static] => Array (
        [example] => 10
    )
    [this] => ExampleClass Object()
)

However I cannot for the life of me work out how to capture these values. It is not possible to use any form of property accessor (e.g. $closure->static or $closure->{'static'}) without receiving the following:

PHP Fatal error: Uncaught Error: Closure object cannot have properties in XYZ.

Array access notation obviously does not work either:

PHP Fatal error: Uncaught Error: Cannot use object of type Closure as array in XYZ.

JSON encoding the object, besides this making the values useless were they objects, provides an empty JSON object {} and using the ReflectionFunction class does not provide access to these items.

The closure documentation does not provide any means of accessing these values at all either.

Outside of doing something shameful like output buffering and parsing print_r or similar, I cannot actually see a way to get these values.

Am I missing something obvious?

Note: The use-case is for implementing memoization and these values would be extremely beneficial in identifying whether or not the call matched a previous cached call.

解决方案

It seems you may have overlooked some of the ReflectionFunction methods.

Take a look at the ReflectionFunction::getClosureThis() method. I tracked it down by looking through the PHP 7 source code by doing a search for the zend_get_closure_this_ptr() which is defined in zend_closures.c.

The manual currently doesn't have a lot of documentation for this function. I'm using 7.0.9; try running this code based on your example:

class ExampleClass {
  private $testProperty = 33;

  public function test() {
    $example = 10;

    return function() use ($example) {
      return $example;
    };
  }
}

$instance = new ExampleClass();
$closure = $instance->test();

print_r($closure);

$func = new ReflectionFunction($closure);
print_r($func->getClosureThis());

You should get output similar to

Closure Object
(
    [static] => Array
        (
            [example] => 10
        )

    [this] => ExampleClass Object
        (
            [testProperty:ExampleClass:private] => 33
        )

)

ExampleClass Object
(
    [testProperty:ExampleClass:private] => 33
)

Regarding the closure static variables, these are returned with ReflectionFunction::getStaticVariables():

php > var_dump($func->getStaticVariables());
array(1) {
  ["example"]=>
  int(10)
}

这篇关于阅读“ this”和“使用” PHP闭包的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆