How do I set the common properties in subclasses?
<p>I'm new to OO programming, and I'm trying to understand how to assign values to common properties from subclasses in a proper manner. I always end up doing it in different ways every time, so I thought it would be a good idea to ask you how you normally do.</p>

<p>Let's say that I have a parent class, <em>Fruit</em> ,and two subclasses <em>Apple</em> and <em>Banana</em>.</p>

<p>
    

<pre><code>abstract class Fruit {

    protected $color; //empty property

    protected function getColor(){
        return $this-&gt;color;
    }
}

class Apple extends Fruit {
    protected $color = 'red';
}

class Banana extends Fruit {
    protected $color = 'yellow';
}

?&gt;
</code></pre>

<p>Each subclass MUST have the property <em>color</em>, which will be used by, let's say, the function <em>getColor</em> in the superclass.</p>

<p>But how do I deal with the property?</p>

<ol>
<li><p>I define the empty variable $color in the parent. Then I set the variable $color in the subclasses, and pass them to the parent by parent::__construct, to assign the value to the property in the parent.</p></li>
<li><p>I define the empty variable $color in the parent. Then I simply use $this->color in the subclass to set the value. </p></li>
<li><p>I define the property in each subclass, and leave it like that. The inherited methods can still use it. </p></li>
</ol>

<p>I'd like to think that it's best to collect the common properties in the parent class, but it just feels like overkill to write that many lines to set a few values (and if I have lots of properties, there will be lots of function parameters as well). Number two and three works, but it is hard to see that I actually need to define the property at all.</p>

<p>How should I think here? I'm so confused by all the OO way of thinking that I barely write any code at all, haha.</p>

<p>Thank you.</p>
