Search code examples
closuresprivatephp-5.3

Call a private static method from a closure (inside the class)


I am trying to call a private static method of a class from inside a closure inside an other method of this same class, and I can't find the right way to do it... even using use referencing the private method...I am able to reference a private variable and pass it, but no way to reference the private method... $refMethod = array('App','_onEvent'); with call_user_func($refMethod) will throw the method is private... I tried with ReflectionClass with PHP 5.4 version also, (WAMP 32bits), but it says that the getClosure method doesn't exists on the instance :(

class App(){
    static public function start(){
        new Form('myform', array('submit'=>function($form) use($someVar){
            if($anyCondition){
                // want to call private self::_onEvent here : any suggestion ?
            }
        }));
    }
    static private function _onEvent(){
        // this is my very private part
    }
}

Well I know quite closure has no scope but so...any way to pass the private context (because the closure is inside the class) for accomplish something like this ? Thanks for any lights !

EDIT : I exactly want to do this answer but this just throw the great

Cannot access self:: when no class scope is active


Solution

  • Ok dudes,

    it works better with PHP 5.4, my upgrade was not ok, I finally get rid of boring complications when upgrading php version, and now the straight solution works :

    calling directly self::privateMethod() won't throw the 'self is nothing in closure...' anymore

    Appart from that, did you know that from now (5.4), (0 == 'anystring') is TRUE now, duh ! need to use strict equal everywhere now, wonderfull upgrate :-s