So I am working with a settings class that extends a base settings class that would be similar to a "global settings". There are several services and each service has its own settings class that extends the abstract base settings class. The abstract base settings class corresponds to settings that are shared between services. So first I will illustrate with an example below and then I will define the problem:
example:
abstract class BaseSettings {
protected $settingA;
protected $settingB;
}
class MyServiceSettings extends BaseSettings {
private $settingC;
public $settingD;
}
problem:
If I create a ReflectionClass instance like so..
$reflect = new ReflectionClass($this);
..from either the MyServiceSettings class or the BaseSettings class (because obviously you can't have an instance of an abstract class), $reflect->getProperties() will always return the properties of both MyServiceSettings and the BaseSettings (and I guess that's appropriate because we're really working with a single concrete class)
Now I am sure I could create an empty class that extends the abstract BaseClass to figure out which properties go where (or just get rid of the abstract and create an instance of the BaseClass), but that seems rather messy, so I am wondering if there is, perhaps, a more elegant way to determine which properties belong to the parent class and which belong to the child class?
If you are curious why I am even doing this - I am serializing the settings to a .json file for the purposes of a recovery feature I added so that one can continue from the last atomic operation that last successfully completed. Why I have to do it this way - limitation of the environment.
I would get it like this:
$class = new ReflectionClass('Some_class_name');
$properties = array_filter($class->getProperties(), function($prop) use($class){
return $prop->getDeclaringClass()->getName() == $class->getName();
});
So basically get all properties and iterate through them checking if they were declared in class we reflected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With