Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 5.3.0 USE keyword -- how to backport in 5.2?

Tags:

php

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

like image 687
Brob Avatar asked May 03 '11 11:05

Brob


2 Answers

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'));
like image 93
Stefan Gehrig Avatar answered Oct 08 '22 01:10

Stefan Gehrig


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);

?

like image 31
binaryLV Avatar answered Oct 08 '22 00:10

binaryLV