function with($val, callable $callback = null) : $val | WithProxy
Passes $val
through $callback
and returns $val
.
Returns a WithProxy
instead if $callback
= null.
composer require adrianschubek/with
$user = with(UserRepository::findById(123), function (User $us) {
$us->setBalance(10);
$us->sendConfirmation();
});
$deletedUser = with(UserRepository::findById(123), fn(User $user) => $user->deleteAccount());
// sendWelcomeMail() is a method of User.
$randomUser = with(UserRepository::createRandomUser())->sendWelcomeMail();