How do I bind a closure created inside a static function to an instance How do I bind a closure created inside a static function to an instance php php

How do I bind a closure created inside a static function to an instance


As i said on my comment it seems that you can't change the "$this" from a closure that comes from a static context. "Static closures cannot have any bound object (the value of the parameter newthis should be NULL), but this function can nevertheless be used to change their class scope."I guess you will have to make something like this:

    class RegularClass {        private $name = 'REGULAR';    }    class Holder{        public function getFunc(){            $func = function ()            {                // this is a static function unfortunately                // try to access properties of bound instance                echo $this->name;            };            return $func;        }    }    class StaticFunctions {        public static function doStuff()        {            $rc = new RegularClass();            $h=new Holder();            $bfunc = Closure::bind($h->getFunc(), $rc, 'RegularClass');            $bfunc();        }    }    StaticFunctions::doStuff();