tags:

views:

33

answers:

2

Hi all.

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

+1  A: 

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.

Matt Way
I also prefer this style, since you can combine multiple operations and only save it once to the database, like this: `$offer->increaseImpressions(); [some other code] $offer->setName('Bananas!'); [some other code] $offer->save();`
Jan Fabry
Also, this will prevent surprises: if you do `$offer->setName('Bananas!')`, then `$offer->increaseImpressions()` but finally decide not to save it (for whatever reason), the name will still have been overwritten in the database.
Jan Fabry
A: 

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?

timmy