PHP 5.3.0 USE identifier
I asked a question yesterday abo开发者_StackOverflow社区ut the USE identifier and it was answered PHP 5.3.0 USE keyword -- how to backport in 5.2?.
However I've had to extend my script to do this twice and unsure how I accommodate both
$available_event_objects = array_filter($event_objects, function ($event_object) use ($week_events) {
// keep if the event is not in $week_events
return !in_array($event_object, $week_events);
});`
and
$calendar_weeks[$week_count][$calendar_date] = array_filter($available_event_objects, function ($event_object) use ($date_pointer) {
// keep if the event is happening on this day
return ($date_pointer >= $event_object->start_date && $date_pointer <= $event_object->end_date);
});`
How can I change this to get it to work in 5.2.9?
Can someone point me in the right direction??
Cheers
PHP did not have anonymous functions before 5.3. You must use any of the callback types instead. Because this gets more difficult and is not very idiomatic with use cases such as yours, I would advise you to apply an imperative programming style instead.
$available_event_objects = array();
foreach ($event_objects as $event_object) {
if (in_array($event_object, $week_events)) {
$available_event_objects[] = $event_object;
}
}
That said, for this case you can freely use array_intersect
, ie. $available_event_objects = array_intersect($week_events, $event_objects);
It's execeptionally sketchily covered in the manual http://www.php.net/manual/en/functions.anonymous.php under "closures".
What use ($var)
does is share a variable between the anonymous function and the parent scope. Usually it will just keep the initial value, and turn that parameter practically into sort of an static variable.
To turn it into a PHP 5.2 compatible construct, it is always best to turn the closures into static callback functions. Instead of = function () {}
write an ordinary declaration:
function cb_event_filter_week($event_object) {
A very non-pretty way would be to share the closure/use
variable via the global scope instead. For that rewrite the function to
function cb_event_filter_week($event_object) {
global $week_events;
You will have to do the same in the parent function, also to initialize it! And it is highly advisable to give that variable a significantly more unique name. A nicer alternative here would be a static variable, if you only need to invoke this callback function at one point (!) in the application flow:
function cb_event_filter_week($event_object) {
static $week_events = 0;
Really depends on how it is utilized. But in either case you can then write = array_filter($event_objects, "cb_event_filter_week")
for using them in PHP 5.2
Updated answer:
While the answer in the original question is correct, and does allow you to easily use array_filter in php 5.2, without closures; it will be easier to simply do a for loop:
$output = array_filter($input, function($input) use ($stuff) { return /* condition */ } );
Changes to :
$output = array();
foreach($input as $key=>$value) {
if (/* condition */) {
$output[$key] = $value;
}
}
精彩评论