开发者

Propel - best practice for implementing a method - shall I save the object inside or outside the method

开发者 https://www.devze.com 2023-01-18 14:03 出处:网络
I am using Propel, a PHP ORM. I really like it. I love to be able to write this code: $offer->setTitle(\'20% off everything\')->save();

I am using Propel, a PHP ORM.

I really like it. I love to be able to write this code:

$offer->setTitle('20% off everything')->save();

But what about if I want to create a increaseImpressions method?.

Would you use开发者_运维百科 it like this:

$offer->increaseImpressions()

or like this:

$offer->increaseImpressions()->save()

?

In other words, would you save the object inside or outside the increaseImpressions method? Can you also please tell me why?

I guess this is a question about OOP.

Thanks, Dan


I would suggest the the second example:

$offer->increaseImpressions()->save()

Essentially, the increaseImpressions method is just another setter. Calling save outside the increaseImpressions method keeps it consistent with the rest of the setter methods.


For something as important as logging impressions it might make sense to make the operation atomic and put the save inside the method. Do you work with any developers you respect whose advice you could ask?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号