|
Posted by Toby A Inkster on 08/24/07 08:08
burgermeister01@gmail.com wrote:
> I needed to add an array class member to an object. It was exactly the
> same as another class member, except that one array stored regular
> products, and the other stored free promotional products. As such, I
> needed a way to add products to the new, free array. Since all the logic
> was the same between the two arrays aside from the price, I had a few
> different options to do this.
Frankly I think all the options you outlined expose too much of the inner
workings of your class. What happens when you add a third category of
products (products that cost money, free products & products we have to
pay you to take away!)
A better solution would be something like this:
public function add_product (Product $p)
{
if ($p->price==0)
$this->free_products[] = $p;
else
$this->products[] = $p;
}
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 64 days, 11:44.]
TrivialEncoder/0.2
http://tobyinkster.co.uk/blog/2007/08/19/trivial-encoder/
Navigation:
[Reply to this message]
|