Access object protected properties
Created 8 years ago by adnanWell, sometimes you just needed to access the value of a protected property of a class that you can't modify, .. out of absolute necessity,
Here's one one way of doing it,
/**
* Read a protected property value in a class instance
*
* @param string or object $instance, the class string or an instance
* @param string $property, property name
* @return mixed
*/
function read_protected_property($instance, $property)
{
return ((array) is_string($instance) ? app($instance) : $instance)[chr(0).'*'.chr(0) . $property];
}
You can pass it either an object or a class string,
$IconRegistry = read_protected_property('Anomaly\Streams\Platform\Ui\Icon\IconRegistry', 'icons');
I'm writing a simple module that would allow users to easily extend the Icons Registry, will announce soon,
Thanks for that Ryan, love the silly goose example, hh ..
What I wanted was to manage the Icons Registry from the Database, so a Module would need access to the list of icons in IconRegistry when running the seeder, .. for that, there's no other way than having to access a protected property,
Well, you don't want to see my initial attempts at this, .. file_get_contents
, eval
, regx
would give you an idea of how messy that was hh
You can extend the icon registry yourself by resolving and using: https://github.com/anomalylabs/streams-platform/blob/master/src/Ui/Icon/IconRegistry.php#L175
Silly goose.
Stuff it into a service provider or something somewhere.