|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-04-07 06:59 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 00:00:01 2025 UTC |
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.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.