Bug #69391 [Nab]: Bug

From: Date: Tue, 07 Apr 2015 07:35:31 +0000
Subject: Bug #69391 [Nab]: Bug
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-191869@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69391&edit=1 ID: 69391 Updated by: rasmus@php.net Reported by: testing1932 at gmail dot com Summary: Bug Status: Not a bug Type: Bug Package: *General Issues PHP Version: 5.6.7 Block user comment: N Private report: N New Comment: You should read http://php.net/manual/en/language.oop5.visibility.php carefully. You are confusing private and protected here. Private properties are not visible to derived classes. You want to mark it protected if you want it to be visible to a derived class. Previous Comments: ------------------------------------------------------------------------ [2015-04-07 07:29:01] testing1932 at gmail dot com Hi, Please check this code: class A { private $tempVar=10; function funcA() { echo $this->tempVar; } } class B extends A { private $tempVar=50; } $obj=new B; $obj->funcA(); So, then why it is not printing 50. It is still printing 10. When we derived a class, parent's class method(s) become available within the derived class so $this->tempVar should print the value that has been assigned within the derived class not that value that is in the parent class. ------------------------------------------------------------------------ [2015-04-07 07:16:04] farhan dot shahid001 at gmail dot com Hi, Please check this code: class A { private $tempVar=10; function funcA() { echo $this->tempVar; } } class B extends A { private $tempVar=50; } $obj=new B; $obj->funcA(); So, then why it is not printing 50. It is still printing 10. When we derived a class, parent's class method(s) become available within the derived class so $this->tempVar should print the value that has been assigned within the derived class not that value that is in the parent class. ------------------------------------------------------------------------ [2015-04-07 07:04:20] aharvey@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php funcA() is still a method defined on A, so any private properties within A (such as $tempVar) are accessible to it. This is by design. ------------------------------------------------------------------------ [2015-04-07 06:59:52] testing1932 at gmail dot com Description: ------------ The private members are accessible within the Derived Classes. Test script: --------------- class A { private $tempVar=10; function funcA() { $this->tempVar=20; echo $this->tempVar; } } class B extends A { } $obj=new B; $obj->funcA(); Expected result: ---------------- It should give an error because the property $tempVar is private and should not be available to derived classes. Actual result: -------------- It is printing the value of $tempVar. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=69391&edit=1

« previous php.bugs (#191869) next »