In PHP how can I access a ":private" array in an object? In PHP how can I access a ":private" array in an object? wordpress wordpress

In PHP how can I access a ":private" array in an object?


If you dont want to touch the core files, then you have to use Reflection:

$adminBar = new WP_Admin_Bar();$reflector = new ReflectionObject($adminBar);$nodes = $reflector->getProperty('nodes');$nodes->setAccessible(true);print_r($nodes->getValue($adminBar));

The hackish alternative would be casting the object to array and then doing:

$adminbar = (array) new WP_Admin_Bar;$nodes = $adminbar[chr(0) . 'WP_Admin_Bar' . chr(0) . 'nodes'];print_r($nodes);


Change them to protected member variables and extend the class.

Whoever wrote the class with private members effectively made the class "final". Which goes to show that you should always write your members as protected, unless there's a really, REALLY good reason to do otherwise.

Hope that helps...


If I understand correctly your question, you're asking if you can access an object's private variables, but I guess you know you can't unless there's a method for it in the class, so this may be a trivial non-useful answer, but just in case:

Look at the class' code. Does it have any method to retrieve those variables, like get_nodes(),get_root(), etc? If not you have 3 alternatives: recode the class set the vars public, recode the class and add the methods, or recode the class and set the variables protected, then create a new class extending the parent one with those methods (I recomend this one, since it has less impact on the parent class).

Anyway if you can't recode the class and it has no get methods you won't be able to access those private variables.