veilig
veilig

Reputation: 5135

php object oriented visibility

I'm a little confused about this paragraph on OO visibilty in PHP. was curious if someone could explain it to me. examples would be GREAT! my brain is not thinking clear.

http://www.php.net/manual/en/language.oop5.visibility.php

The first paragraph reads

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.

how can a parent class access a childs class member?

Upvotes: 5

Views: 800

Answers (4)

James.Valon
James.Valon

Reputation: 81

Protected is a type of visibility which makes properties and methods declared protected available in the child classes of the declared class.

class Parent {
    public $name = 'MyName';
    protected $age = 20;
    private $school = 'MySchool';
}
class Child extends Parent {
   public function __construct() {
       echo $this -> name; // valid as public
       echo $this -> age; // valid as protected
       echo $this -> school; // invalid as private
   }
}

There you understand protected is something that used in inheritance.

Upvotes: 0

parvas
parvas

Reputation: 11

public scope: property (method, variable etc) can be accessed from any class in any file.

class Example {
    public $foo;
}

$example = new Example;
$example->foo = 3; // everything OK

private scope: property can only be accessed only by same class.

class Example {
    private $foo;
}

class Child_Class extends Example {

    public function some_method()
    {
        parent::foo = 3; // raises error
    }
}

protected scope: property can only be accessed by same class or by other classes that extend it.

class Example {
    protected $foo;
}

class Child_Class extends Example {

    public function some_method()
    {
        parent::foo = 3; // this is OK
    }
}

It all has to do with a technique named encapsulation, in which you must not allow a class member's state or behavior to be changed outside the class. http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)

Upvotes: 1

mario
mario

Reputation: 145482

PHP is an interpreted language. Properties are resolved at runtime, not at the compiling stage. And access modifiers are just checked when a property is accessed.

It makes no difference if you ad-hoc inject a new (undeclared) property so it becomes public, or if you declare a protected property in an inherited class.

The private really only affects the accessibility from the outside. The ->name resolving at runtime works regardless of that. And the PHP runtime simply doesn't prope if the property declaration was made for the current object instances class. (Unlike for private declarations.)

Upvotes: 1

deceze
deceze

Reputation: 522016

That's how:

class A {
    public function test() {
        $b = new B;
        echo $b->foo;
    }
}

class B extends A {
    protected $foo = 'bar';
}

$a = new A;
$a->test();

Upvotes: 5

Related Questions