8

I have some code that has been written in for php 5.3.0 using the USE function within PHP

can someone help me change this to work for 5.2.9?

$available  = array_filter($objects, function ($object) use ($week) { 
    return !in_array($object, $week);
});

thanks for the help

hakre
  • 193,403
  • 52
  • 435
  • 836
Brob
  • 665
  • 2
  • 13
  • 23
  • This may help you: http://stackoverflow.com/questions/1065188/in-php-5-3-0-what-is-the-function-use-identifier-should-a-sane-programmer-use – zaf May 03 '11 at 12:01
  • 2
    You probably shouldn't call `use()` a function, as its a little confusing. – alex May 03 '11 at 12:08

4 Answers4

8

Not nice, but this would be an equivalent implementation.

class MyWeekFilter {
    protected $_week;

    public function __construct($week) {
        $this->_week = $week;
    }

    public function filter($object) {
        return !in_array($object, $this->_week);
    }
}

$filter    = new MyWeekFilter($week);
$available = array_filter($objects, array($filter, 'filter'));
Shoe
  • 74,840
  • 36
  • 166
  • 272
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
1

Is there any difference between author's code

$available = array_filter($objects, function ($object) use ($week) { 
    return !in_array($object, $week);
});

and

$available = array_diff($objects, $week);

?

binaryLV
  • 9,002
  • 2
  • 40
  • 42
  • 2
    The author may have cut down the problem space to a simple example that is easier to follow. – dland Feb 14 '12 at 14:36
0
$available  = array_filter($objects, create_function('$object', '
    $week = '.var_export($week,true).';
    return !in_array($object, $week);
'));
newacct
  • 119,665
  • 29
  • 163
  • 224
-3

Try this:

$week = array(...); // defined and instantiated before...

function callback($object) {
    return !in_array($object, $week);
}
$available  = array_filter($objects, "callback");
shadyyx
  • 15,825
  • 6
  • 60
  • 95