Reputation: 3081
class test
{
private $foo;
protected $bar;
public function toarray()
{
return get_object_var($this);
}
class inheritedTest extends test
{
private $baz;
protected $baf;
}
$test=new Test();
test->toarray(); //does acccess private, protected
$itest= new inhertiedTest();
$itest->toArray(); // does access protected but not private
if i override toArray()
and call parent toArray()
it works ok. It seems like get_object_var()
works on declare context. In this case Test
properties are visible. what is happening? how to make it work without overriding toArray()
?
Upvotes: 1
Views: 4578
Reputation: 212412
Quoting from the manual:
Gets the accessible non-static properties of the given object according to scope.
(my emphasis)
If you really need access to private vars in a parent class, you'll need to use reflection
EDIT
An alternative to edit would be to override toArray() in your child class, and call the parent toArray() through that.
Upvotes: 3