J
J
jeckhummer2015-06-09 18:24:54
PHP
jeckhummer, 2015-06-09 18:24:54

How to properly get rid of relationships with unnecessary properties of the parent class inside the derived class?

Class A has a private property $prop , which is initialized in the constructor using a protected method:

class A
{
  private $prop;
  ...

  public function __construct(array &$config)
  {
    if (isset($config['prop'])) {
      $this->setProp($config['prop']);
    }
    ...
  }

  protected function setProp($value)
  {
    $this->prop = $value;
  }
  ...
}

Why using a protected method?
For example, I want the $prop property to be set externally (via the config array) only once during the creation of the class object. If not protected , then the setProp() method is a classic setter. Why is it needed? In order to be able to override it in the derived class B :
class B extends A
{
  // ...
  public function __construct(array &$config)
  {
    parent::__construct($config);
    // инициализация свойств характерных только для класса B
    // ...
  }

  final protected function setProp(){}
  // ...
}

Thus, assignment will not occur. It would seem that if you do not pass in the not null element with the " prop
" key in the configuration array , then there will be no call to setProp() . Suppose this array is required to initialize multiple objects and is created one way and one time. Such a restriction is justified if getting values ​​for it is a time-consuming task (which is why, by the way, I pass it by reference in the constructor argument). Therefore, each object that uses it must take from it only the necessary data. Then the inability to pass the "correct" array to the constructor becomes justified.
Another solution is to rewrite the constructor in the descendant class without using the parent constructor. But, in my opinion, this will lead to frank copy-paste.
Can you suggest a better solution than supplying a stub instead of a method in the child?
In principle, I don't really care that class B has some unnecessary $prop property (even if initialized) if I don't use it. But maybe you should be worried? Has anyone encountered any problems or inconveniences in this regard?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-06-09
@jeckhummer

If your $prop property is private, then declare setProp private too. So neither the method nor the property will be available in descendant classes.
If you don't know if the $prop property will be used in descendants, it's better to make it protected, just like the setProp() method. And then you shouldn't use final protected function setProp(), you don't know how this property can be used in class C extends B.
PS I try to use protected properties, as it gives more control over these descendants. At the same time, if the descendant does not need any property, it simply does not access it.

H
He11ion, 2015-06-09
@He11ion

unset($this->prop);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question