PHP 5.x and PHP 7 closure different behavioral issues

Source: Internet
Author: User

  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.