I have a property that I use to define a set of fields that are used on the class.
Eventually, when the class is extended, PHP allows you to redefine this property, but I don’t want to allow that, because fields available on parent may be lost on overloading.
This is an example:
class A {
protected $_fields = array('a', 'b');
}
class B extends A {
protected $_fields = array('c', 'd'); // I'm loosing 'a' and 'b' fields
}
Can I check at construction time if the property has been overloaded? Maybe by using a reflection method?
If you don’t want to allow the extending class to redefine the property, define the property as private. A private property is only accessible inside the defining class, and any redefinition of the property in the class that extends the parent will not change the value the parent class accesses.
Will output:
.. since the first access will read the property in the parent class (which can’t be overridden in the class inheriting the value), and the extender will still be able to get the value of any local property, regardless of the naming in the parent class.