Hi
I was wondering how some of you guys would approach a problem I have at the moment.
I have a base product class, which I have began to extend with a number of different product types inheriting the product class.
Using inheritance I'm therefore able to amend the new product types business logic to overide the base class methods where different business logic applies.
What I'm now beginning to find is that as more and more business rules are applied to the product specialisations I have a number of different methods which don't sit with the base class.
For instance:
As you can see from the example above, whilst I have the same method (getPrice) in Product1 as I do the Product class, I have additional methods in Product1.Code:class Product { function __construct() { $this->price = 1; } public function getPrice() { return $this->price; } } class Product1 extends Product { function __construct() { parent::__construct(); } public function getPrice() { return $this->price + 1; } public function getNetPrice() { return $this->price - 100; } }
Whilst this isn't a problem if all I do is interact with only Product1 objects, how would I be best to approach this if I had an array of 'Product' objects (containing both Product, Product1) and wanted to call the getPrice method for Product objects and getNetPrice method for Product1 objects. Sorry if this sounds convoluted.
I guess a simple approach would be to add stub methods to the Product class however this strikes me as clumsy and this would need to be reflected across every other product specialisation.
Would a better approach to use something like reflection?







Bookmarks