PHP 'Trick' To Remove Method Parameters (with Setters)

preview_player
Показать описание
If you need to add or remove parameter to some service method, it's usually a pain - you need to change the method signature, double-check its usage in other classes etc. I want to share a "pattern" will help you to avoid that pain.

- - - - -

- - - - -
Рекомендации по теме
Комментарии
Автор

a phpstorm hint: in the popup where you choose properties of which you want to generate setters, there is an option checkbox on the bottom labelled 'Fluent setters' allowing the generation of the same exact code.

nacerali
Автор

Good reminder. Also useful where some attributes can be ommitted. Avoids having 'placeholders' in the method call. Also useful where you want more complex default values, eg default tax needs to come from customer model instead of being defaulted in the method signature

MarkSnape
Автор

I found this PHP trick very useful in a development that was giving me some headaches. Because you had to methodly call the values that will return other methods and then divide those values between the parameters of your parent methods. In the end I was able to achieve the goal as recommended by Povilas in this video. Thanks a lot!!

numenor
Автор

How beautiful use case of builder pattern, absolutely useful

alexisarcegomez
Автор

Is BAD idea! For this was inveted dto and value objects. App services are hasnt state

kirchann
Автор

One extra benefit that wasn't mentioned - you can have all or any of that properties set to some defaults. They can be set dynamically depending on, say, geolocation (hello price parity) etc.

IhorVorotnov
Автор

What if you're processing things in a loop and there's an optional parameter that you only use in some cases? Now that parameter will persist across iterations, so you have to explicitly reset it to its default value when not used, so it's not really optional anymore.

Also, let's say after the loop you have a couple more places where the same service is used and you add a line calling another setter in the loop. This change affects not just the loop, but also the rest of the scope where the same service is used.

Seems like quite a bit of boilerplate will build up around this technique (resetting params or creating new instances of the service) as well as opportunities for bugs.

rafalg
Автор

great video, thanks for sharing both the article and video.

kalemaarnold
Автор

Thank you for nice tip. What, would you do, if your service has 5 functions and each function can have 5 parameters? It can get messy with setters. Maybe my choice is not right, but i prefer to use array for additional parameters.

linasgutauskas
Автор

I feel like it's still the same amount of work 😅 but it definitely helps if called elsewhere

JamesAutoDude
Автор

Why are you doing something like this? Stateless services. Use Data Transfer Objects (DTO) or Value Objects (VO) instead :(

pawelpason
Автор

It's a good trick but in the end yout get more code...but indeed i like it more than parameters, with chaining methods, it's more readable and understandable.

Spyder
Автор

Its one of benefit of chaining methods 🙂

sssurii
Автор

But why you dont use just like function something($foo = 0, $bar = 0){ calculation goes here} you would write less code?

olegrudev
Автор

:) Long time use this method . But still nice video

ValkoValkov
Автор

I don't see any benefit in this way of coding. You still need to find all usages of the function and ass the set method if you are adding a new parameter. Also, you mentioned that one of the benefits is you can not set one of the parameters and it still doesn't throw any error. I don't see it as a benefit, but it is actually opening room for mistakes. If you forget adding the new parameter, you'd better get an error, rather than having a miscalculated value. For the time you don't want to set one of the values, you can have that parameter optional with a default value in the method signature. Also, you are writing a lot more code to achieve the same result!

Bykv
Автор

This doesn't make sense, why you just don't pass order instance to this calculate method and there access discount price etc? If you want to calculate price on many things - introduce interface and simple as that.

Jurigag
Автор

we don't need to generate setters

We can you them in public function and then use like this in any funcion of the class

$this->discount;

Also we can make public $discount=0;

So, In the controller
We need to do this
$pricingService->discount = $order->discount;

Please correct me if i am wrong.

sujit
Автор

What if some of private params is missing?

titov