Answer the question
In order to leave comments, you need to log in
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;
}
...
}
class B extends A
{
// ...
public function __construct(array &$config)
{
parent::__construct($config);
// инициализация свойств характерных только для класса B
// ...
}
final protected function setProp(){}
// ...
}
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question