Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How exactly does the `tap` function work?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading