views:

188

answers:

4
+1  Q: 

Php Inheritance

+5  A: 

OOP5 Visibility

hsz
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.
Ben Reisner
+6  A: 

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.

Gordon
+1 for actually showing the explanation
NateDSaint
+2  A: 

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.

fireeyedboy
+1  A: 

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.

promethean