1

Derived classes can override the values of inherited properties either by initializing them in the property declaration, or by setting their value in the constructor. The result is the same either way.

Is there a semantic difference, or any obvious advantage to either method?
Or is it just a matter of readability and personal preference?

Here is an (admittedly contrived) example to illustrate the two ways:
IdNumber objects are specialized Number objects. Among other differences, they set a specific default minimum value, where the parent class doesn't:

class Number
{
    protected $value;
    protected $minValue;
    public function __construct ($value)
    {
        $this->value = $value;
    }
    public function setMinValue ($min)
    {
        $this->minValue = $min;
    }
    public function isValid ()
    {
        if (isset($this->minValue)) {
            return $this->value >= $this->minValue;
        } else {
            return true;
        }
    }
}

class IdNumberA extends Number
{
    protected $minValue = 1;  //
    /* ...plus more specialized methods... */
}

class IdNumberB extends Number
{
    public function __construct ($value)
    {
        $this->minValue = 1;
        parent::__construct($value);
    }
    /* ...plus more specialized methods... */
}
Zilk
  • 8,917
  • 7
  • 36
  • 44
  • One advantage of the way used in `IdNumberA` that just occurred to me is that the default value can be easily documented for use with PHPDocumentor or other tools. – Zilk Sep 11 '15 at 22:09
  • 1
    In this case you probably should go with composition instead of inheritance, to adhere to the Open-Closed-Principle. Also, this topic has been discussed many times, so you can simply google for "inheritance breaks encapsulation" – Yang Sep 12 '15 at 06:33
  • Inheritance vs composition can be an interesting philosophical discussion, but it's a little off-topic here. If you found this specific question discussed many times, I would be grateful for links. – Zilk Sep 12 '15 at 09:35
  • Here are some links: [One](http://stackoverflow.com/questions/24551/best-practice-initialize-class-fields-in-constructor-or-at-declaration), [Two](http://stackoverflow.com/questions/1994218/should-i-instantiate-instance-variables-on-declaration-or-in-the-constructor), [Three](http://stackoverflow.com/questions/3918578/should-i-initialize-variable-within-constructor-or-outside-constructor) – Yang Sep 12 '15 at 09:47
  • And the reason, I mentioned Inheritance vs composition is to warn you to be careful in order not to break the Open-Closed-Principle (since your second example is tempting to do so). – Yang Sep 12 '15 at 09:49
  • Thanks for the links, although none of them address inherited properties. The difference between PHP and Java/C# is also significant: PHP can only initialize simple values (scalars or array literals) outside of the constructor. Something like `private Random myRand = new Random();` is (currently) not even possible in PHP. I'm well aware of the Open/closed principle; perhaps the quick&dirty example I gave was too distracting in this regard. – Zilk Sep 12 '15 at 11:03

0 Answers0