phparrayslaravelmacros

PHP references doesn't work in Laravel macros


Trying to make a helper method for array that replaces key into given value without return using reference but it doesn't work.

Arr::macro('replaceKey', function (string $from, string $into, array &$inside) {
    if (! array_key_exists($from, $inside)) {
        throw new Exception("Undefined offset: $from");
    }

    $inside[$into] = $inside[$from];

    unset($inside[$from]);
});

Tried the same thing with trait and simple function and it works.

// inside trait

public function replaceKey(string $from, string $into, array &$inside)
{
    if (! array_key_exists($from, $inside)) {
        throw new Exception("Undefined offset: $from");
    }

    $inside[$into] = $inside[$from];

    unset($inside[$from]);
}

Can anyone explain why?


Solution

  • There is a method call before your anonymous function would be getting called when you call a macro; __callStatic gets called. This takes a method name and an array of parameters passed to the method call.

    It isn't possible from the method signature side of this method, __callStatic, to declare that an element of the parameters array is to be a reference since it only receives an array of all the arguments passed to the non existing method, replaceKey, as an argument.

    You are getting a reference to a copy of the array you are passing to your macro method call in your anonymous function.