The same piece of closure code, PHP 7 OK, PHP 5.5.11 (Windows development machine) on the error, think it is PHP 5 bug, the original usage is wrong, record a ~
The original code (the routing part of the framework that wrote itself) was originally written like this:
Full code Address: https://github.com/TF-Joynic/Hydrogen/blob/master/lib/Hydrogen/Route/Router.php
1 Public functionAddRule (ruleinterface$routeRule, \closure$callback)2 {3 4 if(false!==$callbackClosure=$callback->bindto ($routeRule,$routeRule)) {5 $routeRule->setcallback ($callbackClosure);6}Else {7 Throw NewInvalidrulecallbackexception (' Invalid callback closure specified! ');8 }9 Ten if(!$routeRule-isterminable ()) { One Array_unshift($this->_rules,$routeRule); A}Else { - $this->_rules[] =$routeRule; - } the - returnSelf::$_instance; -}
It's no problem running under PHP 7.
But when the 5.5.11 is running, the 4th line is thrown Warning: Cannot bind an instance to a static closure
In the $callback Closure is actually:
function $request $response) {}
Preliminary judgment in the 5.5 environment PHP considers it a static function, while PHP 7 runs under the condition that it may be considered a normal function
Modify the code, in a static manner bind closure, PHP 5 will not error:
1 Public functionAddRule (ruleinterface$routeRule, \closure$callback)2 {3 //must bind as a static Closure, otherwise'll raise:Warning:Cannot bind an instance to a static Closure (v5.x) 4 if(false!==$callbackClosure= \closure::bind ($callback,NULL,$routeRule)) {5 $routeRule->setcallback ($callbackClosure);6 }7 /*if (false!== $callbackClosure = $callback->bindto ($routeRule, $routeRule)) {8 $routeRule->setcallback ($callbackClosure);9 }*/ Else {Ten Throw NewInvalidrulecallbackexception (' Invalid callback closure specified! '); One } A - if(!$routeRule-isterminable ()) { - Array_unshift($this->_rules,$routeRule); the}Else { - $this->_rules[] =$routeRule; - } - + returnSelf::$_instance; -}
Specifically is not the above reasons for scrutiny ...
PHP 5.x and PHP 7 closure different behavioral issues