I am using PHP 5.3 stable release and sometimes I encounter very inconsistent behaviours. As far as I know in inheritance all attributes and methods(private, public and protected) in super class are passed child class.
class Foo
{
private $_name = "foo";
}
class Bar extends Foo
{
public function getName()
{
return $this->_name;
}
}
$o = new Bar();
echo $o->getName();
//Notice: Undefined property: Bar::$_name in ...\test.php on line 11
But when Foo::$_name attribute is defined "public" it doesn't give error. PHP has own OO rules???
Thanks
Edit: Now all things are clear. Actually I was thinking in "inheritance" a new class is created and inherits all members independent from its ancestor. I didn't know "accessing" rules and inheritance rules are the same.
Edit According to your comments this snippet should give an error. But it is working.
class Foo
{
private $bar = "baz";
public function getBar()
{
return $this->bar;
}
}
class Bar extends Foo
{}
$o = new Bar;
echo $o->getBar(); //baz