Seeing the examples the when and unless methods don't blow me out of the water.
I don't understand what is wrong with using if statements? Most of the examples could be ternary statements.
I trained myself to write positive if statements, so the unless method is lost on me.
I don't see how the functions make code more readable, maintainable or elegant.
The methods make code even harder to read if you use the third parameter.
return$user->unless($user->isActive(),function(){return"Your account is inactive. Please contact support.";})->otherwise(function(){return"Welcome back!";});// can be written asreturn$user->unless($user->isActive(),fn()=>"Your account is inactive. Please contact support.",fn()=>"Welcome back!");// plain phpreturn$user->isActive()?"Welcome back!":"Your account is inactive. Please contact support.";
yes you can use multiple if statements, ternary or switch cases where you want. However, this when() and unless() is another way of writing it, nothing else.
I can understand the readability argument. A lot of people find a fluent API more readable.
My previous comment was more about preferences, and those are a million in a bunch.
So I want to give more rooted reasons why not to use the methods.
The when an unless methods are two function calls that have no other added value than readability. One of the low hanging fruits during code optimalisation is reducing function calls.
You also are writing more code instead of less because of the anonymous functions.
And you need to start with a class that has a Conditionable trait. I see people using this and make the context of the flow confusing because they bind it to an unrelated instance.
Seeing the examples the when and unless methods don't blow me out of the water.
I don't understand what is wrong with using if statements? Most of the examples could be ternary statements.
I also spotted an error in one of the examples.
I trained myself to write positive if statements, so the unless method is lost on me.
I don't see how the functions make code more readable, maintainable or elegant.
The methods make code even harder to read if you use the third parameter.
Thank you for the post! I will keep writing php.
Hello David,
yes you can use multiple if statements, ternary or switch cases where you want. However, this when() and unless() is another way of writing it, nothing else.
return $user->unless($user->isActive(),
fn() => "Your account is inactive. Please contact support.",
fn() => "Welcome back!"
);
is a cleaner way however, I personally believe that it is not reading friendly, so I do not prefer using it.
I can understand the readability argument. A lot of people find a fluent API more readable.
My previous comment was more about preferences, and those are a million in a bunch.
So I want to give more rooted reasons why not to use the methods.
The when an unless methods are two function calls that have no other added value than readability. One of the low hanging fruits during code optimalisation is reducing function calls.
You also are writing more code instead of less because of the anonymous functions.
And you need to start with a class that has a Conditionable trait. I see people using this and make the context of the flow confusing because they bind it to an unrelated instance.
Yeah thats quite true