From:             
Operating system: Ubuntu Natty
PHP version:      5.4.0alpha3
Package:          Arrays related
Bug Type:         Bug
Bug description:array_map() throws PHP warning if the callback throws an 
exception

Description:
------------
If you map a function on an array using array_map(), and that function (the
callback) then throws an exception, you get a PHP Warning saying "An error
occurred while invoking the map callback". I guess this is sort of
reasonable if an actual error occurred (even though that error would
presumably have reported itself already, so it's superfluous), but an
exception isn't an actual error unless the code that catches the exception
decides it is.

This is annoying me because I'm squashing PHP warnings and notices in my
code, but I can't get rid of this warning. I have a legitimate use case for
throwing an exception from my map function, so I can either rewrite my code
to not use an exception there (which would be extremely awkward) or write a
foreach equivalent of the array_map() call (which would just be ridiculous;
PHP provides array_map() for a reason: so you can write one-liners instead
of repetitive foreach loops), or remove the IMHO misplaced warning in PHP.

I'll dig into the PHP source and see if I can come up with a patch.

Test script:
---------------
error_reporting(E_ALL);

function barf($i) {
        $foo = $bar['baz'];
        throw new Exception('barf');
}

$a = array(1, 2, 3);

try {
        array_map('barf', $a);
} catch(Exception $e) {
        echo $e;
}


Expected result:
----------------
Notice: Undefined variable: bar in
/home/catrope/php-5.4.0alpha3/testcase.php on line 3

exception 'Exception' with message 'barf' in
/home/catrope/php-5.4.0alpha3/testcase.php:3
Stack trace:
#0 [internal function]: barf(1)
#1 /home/catrope/php-5.4.0alpha3/testcase.php(6): array_map('barf', Array)


Actual result:
--------------
Notice: Undefined variable: bar in
/home/catrope/php-5.4.0alpha3/testcase.php on line 3

Warning: array_map(): An error occurred while invoking the map callback in
/home/catrope/php-5.4.0alpha3/testcase.php on line 6
exception 'Exception' with message 'barf' in
/home/catrope/php-5.4.0alpha3/testcase.php:3
Stack trace:
#0 [internal function]: barf(1)
#1 /home/catrope/php-5.4.0alpha3/testcase.php(6): array_map('barf', Array)


-- 
Edit bug report at https://bugs.php.net/bug.php?id=55416&edit=1
-- 
Try a snapshot (PHP 5.4):            
https://bugs.php.net/fix.php?id=55416&r=trysnapshot54
Try a snapshot (PHP 5.3):            
https://bugs.php.net/fix.php?id=55416&r=trysnapshot53
Try a snapshot (trunk):              
https://bugs.php.net/fix.php?id=55416&r=trysnapshottrunk
Fixed in SVN:                        
https://bugs.php.net/fix.php?id=55416&r=fixed
Fixed in SVN and need be documented: 
https://bugs.php.net/fix.php?id=55416&r=needdocs
Fixed in release:                    
https://bugs.php.net/fix.php?id=55416&r=alreadyfixed
Need backtrace:                      
https://bugs.php.net/fix.php?id=55416&r=needtrace
Need Reproduce Script:               
https://bugs.php.net/fix.php?id=55416&r=needscript
Try newer version:                   
https://bugs.php.net/fix.php?id=55416&r=oldversion
Not developer issue:                 
https://bugs.php.net/fix.php?id=55416&r=support
Expected behavior:                   
https://bugs.php.net/fix.php?id=55416&r=notwrong
Not enough info:                     
https://bugs.php.net/fix.php?id=55416&r=notenoughinfo
Submitted twice:                     
https://bugs.php.net/fix.php?id=55416&r=submittedtwice
register_globals:                    
https://bugs.php.net/fix.php?id=55416&r=globals
PHP 4 support discontinued:          
https://bugs.php.net/fix.php?id=55416&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=55416&r=dst
IIS Stability:                       
https://bugs.php.net/fix.php?id=55416&r=isapi
Install GNU Sed:                     
https://bugs.php.net/fix.php?id=55416&r=gnused
Floating point limitations:          
https://bugs.php.net/fix.php?id=55416&r=float
No Zend Extensions:                  
https://bugs.php.net/fix.php?id=55416&r=nozend
MySQL Configuration Error:           
https://bugs.php.net/fix.php?id=55416&r=mysqlcfg

Reply via email to