PHP - How to avoid Parse errors on older servers when using new functions

守給你的承諾、 提交于 2019-12-12 09:54:58

问题


When I use an anonymous function ( but see also note below ) like :

$f = function() use ($out) {
                echo $out;
            };

It produces an parse error on servers where PHP is older than 5.3.0. My software needs to be compatible with unknown servers , but in the same time , I want also to use new functions, so I thought I will add some kind of a version check,

if (o99_php_good() != true){
            $f = function() use ($out) {
                echo $out;
            };
        }

where the o99_php_good() is simply

function o99_php_good(){
    // $ver= ( strnatcmp( phpversion(),'5.3.0' ) >= 0 )? true:false;
    $ver= ( version_compare(PHP_VERSION, '5.3.0') >= 0 )? true:false;
return $ver;
}

But still the error is produced .

Is there a way to somehow "isolate" that part of the code ?

I thought of doing a conditional include() based on a version check , which would probably work, but it will be absurd to make a separate file every time I need to use a function...

Any creative ( or trivial ) solution for that ?

Note : the example here is just a Lambda function, but the problem can occur in every other function which is a new feature and/or might not be supported on some servers ..

Edit I after comment .

Adding a "minimum requirements" list is always good, but it does not resolve the problem.

If one does not want to loose users (not all customers are happy or can upgrade their servers every time I will update a new function ) it will just force me to "fork" my software into 2 (or possibly more ) unmanageable versions..


回答1:


I had exactly the same problem and solved it using eval function :

if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
eval('
function osort(&$array, $prop)
{
    usort($array, function($a, $b) use ($prop) {
        return $a->$prop > $b->$prop ? 1 : -1;
    }); 
}
');
} else {
    // something else...
}



回答2:


The only thing I can think of is to make a build script which people will have to run to make it compatible with lower versions.

So in case of anonymous methods, the script will loop through all the PHP files, looking for anonymous methods:

$f = function($a) use($out) {
    echo $a . $out;
};

and replace them with create_function for < 5.3:

$f = create_function('$a', '
    global $out;
    echo $a . $out;
');



回答3:


Anonymous functions came with PHP 5.3.0.
My first thing to do would be to replace with one of the following:

$my_print_r = "my_print_r";
$my_print_r();

or

$my_print_r = create_function('$a','print_r($a);');


UPDATE:

i would go with the first method... It would give me enough control for creating my own function versions and in much easier procedure than create_function, regardless of PHP version. I never had any problems with that approach. In fact i once built a whole multidimensional array of functions, that was easily manageable and able to add/remove functions, think of that.. That system was also used by other users, they were able to add their functions too in a way.




回答4:


use include

if (o99_php_good() != true){
include 'new_func.php';
        }

new_func.php:

        $f = function() use ($out) {
            echo $out;
        };


来源:https://stackoverflow.com/questions/16413627/php-how-to-avoid-parse-errors-on-older-servers-when-using-new-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!