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
I think it’s designed to be used with instances of an object, rather than simple types.
Example:
This outputs:
Demo: https://3v4l.org/gfQt0 .
Your version would work just fine with an object instance too:
Again this outputs:
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:
Therefore the
$value
returned from thetap
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 bytap()
to have been updated as well.Laravel
tap()
documentation: https://laravel.com/docs/11.x/helpers#method-tapMore background / examples regarding
tap()
: https://medium.com/@taylorotwell/tap-tap-tap-1fc6fc1f93a6I think you are misunderstanding something.
As stated in https://laravel.com/docs/11.x/helpers#method-tap
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:You will change the
$user
by changing$value
.