php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68686 spurious warning about errors in array_*() callbacks
Submitted: 2014-12-29 14:03 UTC Modified: 2017-12-06 18:06 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: clement dot herreman at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Arrays related
PHP Version: 5.6.4 OS: OS X Maverick, Centos
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: clement dot herreman at gmail dot com
New email:
PHP Version: OS:

 

 [2014-12-29 14:03 UTC] clement dot herreman at gmail dot com
Description:
------------
When throwing an exception is the callback of array_map or array_filter, an E_WARNING is triggered ("Warning: array_filter(): An error occurred while invoking the filter callback in /your/script.php on line XX").

If no error handler is set, then the warning is directly sent to the output.

If an error handler is set via set_error_handler then: 
  * The error handler is not called at all
  * The warning simply disappear and are nowhere to be found

Test script:
---------------
<?php
/* Sample script: `php this.php` to see the warning in the output
 *                `php this.php 1` to try to setup an error handler
 */
if (!empty($argv[1])) {
    set_error_handler(function ($errno, $errstr) {
        printf("(%d) %s\n", $errno, $errstr);
    });
}

function failFilter()
{
    try {
        array_filter([1, 2, 3, 4], function ($n) {
            throw new \Exception('Some exception');
        });
    } catch (\Exception $e) {

    }
}

function failMapException()
{
    try {
        array_map(function ($n) {
            throw new \Exception('oops');
        }, [1, 2, 3]);
    } catch (\Exception $e) {

    }
}

function failMapTriggerError()
{
    try {
        array_map(function ($n) {
            trigger_error('Some error');
        }, [1, 2, 3]);
    } catch (\Exception $e) {

    }
}

failFilter();
failMapException();
failMapTriggerError();

Expected result:
----------------
(1024) Some exception
(1024) Some exception
(1024) Some exception

(1024) oops
(1024) oops
(1024) oops

(1024) Some error
(1024) Some error
(1024) Some error

Actual result:
--------------
cherreman@mlil-cherreman ~ $ php test.php
PHP Warning:  array_filter(): An error occurred while invoking the filter callback in /Users/cherreman/test.php on line 14

Warning: array_filter(): An error occurred while invoking the filter callback in /Users/cherreman/test.php on line 14
PHP Warning:  array_map(): An error occurred while invoking the map callback in /Users/cherreman/test.php on line 25

Warning: array_map(): An error occurred while invoking the map callback in /Users/cherreman/test.php on line 25
PHP Notice:  Some error in /Users/cherreman/test.php on line 35

Notice: Some error in /Users/cherreman/test.php on line 35
PHP Notice:  Some error in /Users/cherreman/test.php on line 35

Notice: Some error in /Users/cherreman/test.php on line 35
PHP Notice:  Some error in /Users/cherreman/test.php on line 35

Notice: Some error in /Users/cherreman/test.php on line 35
cherreman@mlil-cherreman ~ $ php test.php 1
(1024) Some error
(1024) Some error
(1024) Some error

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-06 16:32 UTC] danack@php.net
I'm not going to mark it as a duplicate, as they are really separate issues, but the issue described here should be resolved when the issue https://bugs.php.net/bug.php?id=55416 is fixed.
 [2015-10-30 21:56 UTC] devsoc at gmail dot com
It seems that issue #55416 has not fixed this. The warning still occurs: PHP 5.6.4-4ubuntu6.3 (cli) (built: Sep 29 2015 12:44:47).

array_reduce(['a'], function () { throw new \Exception(); });
 [2016-07-25 11:59 UTC] cmb@php.net
-Summary: set_error_handler cannot catch warning triggered by an exception in a callback +Summary: spurious warning about errors in array_*() callbacks -Status: Open +Status: Verified -Package: Output Control +Package: Arrays related
 [2016-07-25 11:59 UTC] cmb@php.net
For the record: this issue has already been fixed as of PHP 7.0.0,
see <https://3v4l.org/kKKcT#v560> and <https://3v4l.org/sW6Mb>.

The fact that the error handler isn't called in the supplied test
script is expected behavior, because the exception has already
been caught.
 [2017-12-06 18:06 UTC] nikic@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: nikic
 [2017-12-06 18:06 UTC] nikic@php.net
Closing as this is fixed in PHP 7 and 5.6 is out of active support.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 09:01:33 2025 UTC