6

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
6
  • OOP5 Visibility Commented Jan 11, 2010 at 15:15
  • Key thing is that the private tag makes the member variable inaccessible to child instances of that class. if you had declared it as 'protected $_name' it would have worked properly and not been public. Commented Jan 11, 2010 at 15:17
  • 1
    No. Those rules are exactly the same as in Java and C++ where PHP's object model was taken from. Commented Jan 11, 2010 at 15:21
  • 3
    Since getBar() is defined in the parent class, it can access the private member variable. Commented Jan 19, 2010 at 1:07
  • return $this->bar; Here $this refers to Bar object and Bar class has no $bar variable. How could it be? Commented Jan 19, 2010 at 2:37

3 Answers 3

12

From PHP Manual:

The visibility of a property or method can be defined by prefixing the declaration with the keywords public, protected or private. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.

class A
{
    public $prop1;     // accessible from everywhere
    protected $prop2;  // accessible in this and child class
    private $prop3;    // accessible only in this class
}

And No, this is not different from other languages implementing the same keywords.

Regarding your second edit and code snippet:

No, this should not give an error because getBar() is inherited from Foo and Foo has visibility to $bar. If getBar() was defined or overloaded in Bar it would not work. See http://codepad.org/rlSWx7SQ

Sign up to request clarification or add additional context in comments.

Comments

3

Your assumptions aren't correct. Protected and public members are 'passed on'. Private members aren't. To my knowledge this is typical for many OOP languages.

Comments

1

Private methods and variables cannot be accessed by child classes or externally - only by the class itself. Use Protected if you want the variable accessible by the child but inaccessible to outside classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.