note 100931 deleted from language.oop5.visibility by danbrown

From: Date: Tue, 16 Nov 2010 12:14:04 +0000
Subject: note 100931 deleted from language.oop5.visibility by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-174064@lists.php.net to get a copy of this message
Note Submitter: Craig ---- This is in response to ryan lange below. Your example given is not a "quirk" or bug, and behaves as expected. To try explain what's happening, do the following at the end of your example: <?php echo "<pre>"; print_r($child2); echo "</pre>"; ?> This outputs: ChildClass2 Object ( [foo:private] => [foo] => bar ) This demonstrates that ChildClass2 has created a member "foo" with the value "bar" on the fly (ChildClass2::foo, rather than setting ParentClass::foo). The reason for this is because of the private visibility given to ParentClass::foo. The docs state: "Members declared as private may only be accessed by the class that defines the member." This means ChildClass2 in your example never accesses the member defined in the parent class. Since $this->foo doesn't exist to ChildClass2, it created it. ParentClass, on the other hand, only ever accesses its local member, because it's unaware of the existence of ChildClass2::foo. If you want the behaviour of accessing members across in a class and its children, then you should use the protected keyword, not private.

« previous php.notes (#174064) next »