3

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..

Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • It's good idea to make list of minimum software requirements for your software. – Yogesh Suthar May 07 '13 at 07:36
  • 2
    @Yogesh Suthar - yes it is, and I am doing so, but still, I would like to know if there is a solution for this . A list of min requirement will not resolve my problem, but will only force me to "fork" my project into 2 - or alternatively loose users .. – Obmerk Kronen May 07 '13 at 07:40
  • `PHP` is deprecating so many functions and removing then in new release. and if you have used them they will give you fatal error. – Yogesh Suthar May 07 '13 at 07:51
  • Sometimes It is the best to depart from "old version" support. Trust me. It will optimize your efforts. You should be the one endorsing the specs. Afterall it is **your** blood sweat and tears... The rest is marketing... Telling that this way it will work better... – Volkan May 07 '13 at 07:59
  • 1
    I completely disagree that we should toss out so much supported code so often, forcing users into time-consuming upgrades. It's not that hard to maintain legacy compatibility. It's just an OCD tick to want to keep libraries 100% clean. It wasn't so bad until the last 5 years, when WordPress, Drupal, and other major players adopted the mentality of giving everyone tons of work trying to keep sites & plugins working. It's too much to ask, in many cases. For these reasons, I moved away from those types of softwares and push my customers away now as well. – Brandon Elliott Jul 22 '16 at 01:45

4 Answers4

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...
}
Sly
  • 361
  • 2
  • 9
0

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.

  • I was afraid something like this would come up :-) that whole doubt was started after this : http://stackoverflow.com/questions/16401270/php-understanding-create-function-passing-simple-variable .. now what path to take ?? – Obmerk Kronen May 07 '13 at 07:52
  • i would go with the first method... It would give me enough control for creating my own function versions regardless of PHP version. I never had any problems with that approach. –  May 07 '13 at 07:57
0

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;
');
sroes
  • 14,663
  • 1
  • 53
  • 72
0

use include

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

new_func.php:

        $f = function() use ($out) {
            echo $out;
        };
Amir
  • 4,089
  • 4
  • 16
  • 28
  • Thanks for the answer. I already wrote so myself in the question itself , but It is not really a global solution to have X (possibly 100 ) external files every time I need a function .. – Obmerk Kronen May 07 '13 at 08:26