I use the stable version of PHP 5.3, and sometimes I come across very inconsistent behavior. As far as I know in inheritance, all attributes and methods (private, public and protected) in a super class are passed by a child class.
class Foo { private $_name = "foo"; } class Bar extends Foo { public function getName() { return $this->_name; } } $o = new Bar(); echo $o->getName();
But when the name attribute Foo :: $ _ is defined as "public", it does not give an error. PHP has its own OO rules ???
thanks
Edit: Now everything is clear. Actually, I was thinking about “inheritance”, creating a new class and inheriting all members independent of my ancestor. I did not know the “access” rules, and the inheritance rules are the same.
Edit According to your comments, this snippet should give an error. But it works.
class Foo { private $bar = "baz"; public function getBar() { return $this->bar; } } class Bar extends Foo {} $o = new Bar; echo $o->getBar();
inheritance oop php
jsonx
source share