-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.6] Add callback support to "optional" #23688
Conversation
how is this better than a ternary or an if statement? I love the optional helper, but I think it's getting stretched a bit far lately. |
A ternary is insufficient when the value comes from a function/expression. |
Ah ok, so this would be for cases where you might have a result of one function that you want to pass as an argument to another function. I don't think that's a common pattern for me, but that may be because I haven't been looking for it. It would probably be helpful to update the example since the current one would be more succinct as a ternary. There was another PR recently where a contributor wanted to add similar functionality to Personally, I like keeping optional as nothing more than a PHP version of Ruby's |
It will call the https://github.com/laravel/framework/blob/5.6/src/Illuminate/Support/helpers.php#L1121-L1142 Otherwise it returns |
I find it odd that optional($user, function ($user) {
return $user->sendResetPassword();
})->getInvalidProperty->dump; It now would throws exception. |
Do a PR to remove the Optional helper. I'd +1 it. tap tap tap tap dat all day 😄 |
@derekmd the @crynobone I don't see that confusing at all. These are two different uses of the |
Lets you pipe the value into an outside function, with the guarantee that you're not passing it
null
: