php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69391 Bug
Submitted: 2015-04-07 06:59 UTC Modified: 2015-04-07 07:35 UTC
From: testing1932 at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.6.7 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: testing1932 at gmail dot com
New email:
PHP Version: OS:

 

 [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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-07 07:04 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2015-04-07 07:04 UTC] 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 07:16 UTC] 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:29 UTC] testing1932 at gmail dot com
-: farhan dot shahid001 at gmail dot com +: testing1932 at gmail dot com
 [2015-04-07 07:29 UTC] 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:35 UTC] rasmus@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 14:01:34 2024 UTC