Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does array_map throw a warning when the closure raises an exception?

I've recently started programming with PHP again, after a long stint with other languages during which i've developed a more functional style - which i'm hoping to try and maintain.

I've noticed some weird behaviour, which I managed to distill into a testcase that I'm hoping someone can explain.

$func = function($item) {
    if ($item == 0)
        throw new Exception("Can't do 0");
    return $item;
};

try {
    array_map($func, array(1, 2, 3, 0, 5));
} catch (Exception $ex) {
    echo "Couldn't map array";
}

When executing the above code, i see the following output:

Warning: array_map(): An error occurred while invoking the map callback in map_closure.php on line 10 Couldn't map array

I can suppress the error with @ on array_map, but this seems hacky at best.

like image 266
Glenjamin Avatar asked Apr 02 '11 13:04

Glenjamin


People also ask

What happens when an exception is triggered?

This is what normally happens when an exception is triggered: The current code state is saved. The code execution will switch to a predefined (custom) exception handler function.

How to throw exceptions in PHP?

Throwing Exceptions in PHP Throwing a generic exception is almost as simple as it sounds. All it takes is to instantiate an exception object—with the first parameter of the Exception constructor being the error message—and then, "throw" it. The most important thing to take note of is the message.

What is an exception in PHP?

An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.

What is the use of Array_map in PHP?

The array_map() is an inbuilt function in PHP and it helps to modify all elements one or more arrays according to some user-defined condition in an easy manner. It basically, sends each of the elements of an array to a user-defined function and returns an array with new values as modified by that function.


3 Answers

The warning is generated because, put simply, the callback function is not returning normally (due to throwing the Exception). This is just the way that array_map() is coded, if the callback function does not complete its execution. Remember an Exception breaks out of execution immediately, as far as your PHP code is concerned.

As for how to silence the warning, that's entirely up to you. Unfortunately, the warning will be generated and it's your choice to bury it or let it get displayed.

As an aside, maybe your test case was over-simplified but, it would make much more sense to use array_filter() (or perhaps array_reduce()) there.

like image 63
salathe Avatar answered Sep 19 '22 16:09

salathe


As preinhaimer says, array_map makes it really hard for you to see exactly what happened during its execution because it predates exceptions. It would not be practical to change its behavior anymore since that would lead to lots of (poorly-coded) applications breaking; that's life.

If you want a mechanism with which to check if the array_map completed without errors or not, I have posted a detailed answer (with code) to this question which deals with practically the same problem. It's not as easy as try/catch, but you work with what you have.

like image 26
Jon Avatar answered Sep 22 '22 16:09

Jon


Either use @ or a foreach instead of array_map

like image 34
Gildas Avatar answered Sep 20 '22 16:09

Gildas