/with

Useful with() helper

Primary LanguagePHP

with()

Latest Stable Version License

function with($val, callable $callback = null) : $val | WithProxy

Passes $val through $callback and returns $val.
Returns a WithProxy instead if $callback = null.

Installation

composer require adrianschubek/with

Example

Using Arrow function/Closure

$user = with(UserRepository::findById(123), function (User $us) {
    $us->setBalance(10);
    $us->sendConfirmation();
});
$deletedUser = with(UserRepository::findById(123), fn(User $user) => $user->deleteAccount());

Using Proxy

// sendWelcomeMail() is a method of User.
$randomUser = with(UserRepository::createRandomUser())->sendWelcomeMail();