如何在 php 中从闭包对象获取数组


How to get array from a closure object in php

我有一个对象,当var_dump()ed时产生以下内容

object(Closure)#78 (3) {
  ["static"]=>
     array(3) {
        ...
     }
}

有没有办法检索标记为静态的数组?

我尝试了以下方法:

ReflectionClass::getProperty('static');//returns property static does not exit
ReflectionClass::getStaticPropertyValue('static');//Class Closure does not have a property named static
ReflectionClass::getProperties();// returns an empty array
ReflectionClass::getStaticProperties();//returns an empty array
$obj->static;//Closure object cannot have properties
get_obj_vars($obj);//returns an empty array
(array)$obj;//wraps $obj in an array, but does not CONVERT to an array
json_decode(json_encode($obj));//returns object(stdClass)#79 (0) {}

谢谢。

您在此处看到的static属性是'Closure类的实现细节,并且仅在PHP内部。该类捕获此属性中的use d 变量。

您无法访问此属性,并且对'Closure对象唯一可以执行的操作是调用或传递它。

通过反射,您只能分别使用 ReflectionFunctionAbstract::getClosureScopeClassReflectionFunctionAbstract::getClosureThis 访问示波器。

私有静态函数 getRegistrar($callable)    {        if(is_callable($callable))        {            if(is_a($callable,'Closure'))            {                preg_match_all('/''[this''] => (.+) Object/', print_r($callable,true), $matches);                if(count($matches)>1)                    返回$matches[1];            }            else if (is_array($callable) && count($callable)>0 && is_a($callable[0],'Object'))            {                返回 gettype($callable[0]);            }}    }