skip to Main Content

I’m having a hard time understanding how exactly the old version of Laravel’s tap function works, more specifically this:

    function tap($value, $callback)
    {
        $callback($value);

        return $value;
    }

I cannot wrap my head around this line $callback($value);
We are not saving the result to another variable, why ?
Then we are returning the modified value but how is this possible without saving the result of $callback($value); to a variable ?

What I tried is this:

function myTapFunction($value)
{
    tempFn($value);

    return $value;
}

function tempFn($value)
{
    return $value + 5;
}

var_dump( myTapFunction(5) );

but it returns 5, instead of 10. What’s the difference between the original function and my implementation?

2

Answers


  1. I think it’s designed to be used with instances of an object, rather than simple types.

    Example:

    function tap($value, $callback)
    {
        $callback($value);
    
        return $value;
    }
    
    
    class Test {
        public $val = 0;
        public function increment($val) {
            $this->val += $val;
        }
    }
    
    $a = new Test();
    var_dump( tap($a, function($value) { return $value->increment(5);}) );
    

    This outputs:

    object(Test)#1 (1) {
      ["val"]=>
      int(5)
    }
    

    Demo: https://3v4l.org/gfQt0 .

    Your version would work just fine with an object instance too:

    function myTapFunction($value)
    {
        tempFn($value);
    
        return $value;
    }
    
    function tempFn($value)
    {
        return $value->increment(5);
    }
    
    class Test {
        public $val = 0;
        public function increment($val) {
            $this->val += $val;
        }
    }
    
    $a = new Test();
    var_dump( myTapFunction($a) );
    

    Again this outputs:

    object(Test)#1 (1) {
      ["val"]=>
      int(5)
    }
    

    Live demo: https://3v4l.org/vMhfc .

    So I think you’ve misunderstood the use case more than anything.

    The reason the behaviour is different for object instances is because (unlike simple types) object identifiers sent as a function argument still point to the same object – see the PHP objects and references documentation. Relevant quote reproduced here for ease:

    In PHP, an object variable doesn’t contain the object itself as value. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

    Therefore the $value returned from the tap function will be the variable you passed into it. The difference is, with a simple type such as int, that is unaffected by anything which happens inside the closure, whereas an object modified inside the closure points back to the same original object in memory, thereby causing the object identified in the variable returned by tap() to have been updated as well.

    Laravel tap() documentation: https://laravel.com/docs/11.x/helpers#method-tap

    More background / examples regarding tap(): https://medium.com/@taylorotwell/tap-tap-tap-1fc6fc1f93a6

    Login or Signup to reply.
  2. I think you are misunderstanding something.

    As stated in https://laravel.com/docs/11.x/helpers#method-tap

    The $value will be passed to the closure and then be returned by the
    tap function. The return value of the closure is irrelevant

    It will not return the modified value but the value you passed to it.

    Where I think your confusion stems from is the fact that objects are always passed by reference not by value.

    So if you have for example $user that is an object and you pass it to a function like:

    function capitalizeName($value) {
        $value->name = ucfirst($value->name);
    }
    
    $user = (object)['name' => 'john'];
    capitalizeName($user);
    return $user->name; // will return 'John'
    

    You will change the $user by changing $value.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search