1

I have an object which yields the following when var_dump()ed

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

Is there a means by which i can retrieve the array labeled static?

I have tried the following:

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) {}

Thanks.

Ayo Makanjuola
  • 608
  • 7
  • 14
  • 2
    Just access the array as it is a normal property: `print_r($yourVar->static);` – Rizier123 Jul 07 '16 at 08:48
  • @Rizier123: I have also tried that it gives this error: Closure object cannot have properties – Ayo Makanjuola Jul 07 '16 at 08:50
  • Why are you poking around the internals of a closure in the first place? – deceze Jul 07 '16 at 08:55
  • @deceze: A set of rules are defined within it. I want to do some process based on those rules without having to duplicate the rules in an array or something. Which will mean 2 set of rules to maintain. Now why dont i just use arrays alone? Well the closure is generated automatically by the framework i use. I dont have control over that. Hence i'm poking the closure for the ruleset. – Ayo Makanjuola Jul 07 '16 at 09:07
  • The implementation details of closures are incidental and not an official stable API you should depend on. Logically you don't have access to closed-over variables from outside the closure. You should not depend on implementation specific hacks, if one exists at all, to achieve something you logically shouldn't be able to. – deceze Jul 07 '16 at 09:08
  • Thanks all for your opinions. I have figured out another way to access the rule set. So no need to poke into the closure any more. Thanks again! Cheers! – Ayo Makanjuola Jul 07 '16 at 10:10

2 Answers2

1

The static property you see here is an implementation detail of the \Closure class and solely internal to PHP. The class captures the used variables in this property.

You cannot access this property and the only thing you can do with a \Closure object is calling it or passing it around.

Via reflection you can only access the scope using ReflectionFunctionAbstract::getClosureScopeClass and ReflectionFunctionAbstract::getClosureThis respectively.

Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
1
private static function 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)
                    return $matches[1];
            }
            else if (is_array($callable) && count($callable)>0 && is_a($callable[0],'Object'))
            {
                return gettype($callable[0]);
            }


        }
    }