PHP 5.3.0. USE keyword - how to back up in 5.2?

I have code written for php 5.3.0 using the USE function in PHP

can someone help me change this to work in 5.2.9?

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

thanks for the help

+8
php
source share
4 answers

Not good, but it will 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')); 
+8
source share

Is there a difference between author code

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

and

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

?

+1
source share
 $available = array_filter($objects, create_function('$object', ' $week = '.var_export($week,true).'; return !in_array($object, $week); ')); 
0
source share

Try the following:

 $week = array(...); // defined and instantiated before... function callback($object) { return !in_array($object, $week); } $available = array_filter($objects, "callback"); 
-3
source share

All Articles