note 56435 added to language.oop5.visibility

From: Date: Fri, 02 Sep 2005 11:58:26 +0000
Subject: note 56435 added to language.oop5.visibility
Groups: php.notes 
Request: Send a blank email to php-notes+get-94658@lists.php.net to get a copy of this message
When you call parent method from child class, inside parent method $this contains object of child class, but private members depends on from which context you access private property: <?php class A { private $prop = "I am property of A!"; public function s() { echo "\$this is object of class ".get_class($this)." but \$this->prop is '".$this->prop."'\n"; } } class B extends A { private $prop = "I am property of B!"; public function s() { echo "\$this is object of class ".get_class($this)." but \$this->prop is '".$this->prop."'\n"; } public function z() { parent::s(); $this->s(); } } $b = new B(); $b->z(); ?> This will produce: $this is object of class B but $this->prop is 'I am property of A!' $this is object of class B but $this->prop is 'I am property of B!' But when you use members with protected or public visibility - it not depends on context. If you replace "private" to "protected" in example above, you will get "I am property of B!" twice. See ya! ---- Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56435 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56435&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56435&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56435&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56435&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56435&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56435&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56435&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94658) next »