I recently had some time to refactor an old personal project and i wanted to use it as a learning experience on how to best deal with this kind of class.
The problem is my class (item) has 2 genders associated with it (and in the future may have more like a poster of the item, and hats/accessories of the item), each of these genders uses nearly identical methods to get and set things about them (get_productImage, set_price, etc).
I wanted to refactor the class so that instead of using a male and female counterpart for each function, i could have the code written once, and use it twice (or more). Here is (a very simplified example of) what i have so far:
class Item
{
public $ID, $displayDate;
public $male, $female;
public function __construct($ID)
{
//Fancy code to initialize stuff like $displayDate
$male = new Gender('male', $ID, $displayDate);
$female = new Gender('female', $ID, $displayDate);
}
}
class Gender
{
private $ID, $displayDate;
public function get_currentPrice()
{
//The current price of the gender changes based on the display date vs the current date and then adjusts the price accordingly and returns it.
}
}
Is there a way that i can re-write this better? The other problem i have is that the displayDate can (and will be) changed and shuffled around a lot so any changes to it need to be propagated to the gender class as well which is going to make this a huge mess...
If it helps, i am using php 5.4 so any new things added to that can be used here as well.