Skip to content

Broken Function: array_walk_recursive (and more likely others) when using argument reference in callback function #621

@bnowak

Description

@bnowak

Function URL

https://www.php.net/manual/en/function.array-walk-recursive.php

PHP Version

8.4

Safe Version

3.0.0

Description

Hello,

I noticed that passing argument by reference in array_walk_recursive doesn't work correctly with PHP 8.4 (like it worked with PHP 8.3).

Example:

$data = [
    ['foo', 'far'],
    ['bar', 'baz'],
];
array_walk_recursive($data, static function (&$item) {
    $item = 111;
});

var_dump($data); // it's still original [['foo', 'far'], ['bar', 'baz']]

# using native PHP array_walk_recursive directly gives expected [[111, 111], [111, 111]] overwritten by reference values

The reason is probably because different way of wrapping it between PHP versions:

# /generated/8.3/array.php
function array_walk_recursive(&$array, callable $callback, $arg = null): void
{
    error_clear_last();
    if ($arg !== null) {
        $safeResult = \array_walk_recursive($array, $callback, $arg);
    } else {
        $safeResult = \array_walk_recursive($array, $callback);
    }
    if ($safeResult === false) {
        throw ArrayException::createFromPhpError();
    }
}

# /generated/8.4/array.php
function array_walk_recursive()
{
    return \array_walk_recursive(...func_get_args());
}

Using func_get_args() probably doesn't handle reference as expected, so other wrapped functions in this way may be also affected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions