note 56435 deleted from language.oop5.visibility by felipe

From: Date: Mon, 24 Mar 2008 22:58:37 +0000
Subject: note 56435 deleted from language.oop5.visibility by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137515@lists.php.net to get a copy of this message
Note Submitter: gugglegum at gmail dot com ---- 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!

« previous php.notes (#137515) next »