FAQ
Edit report at https://bugs.php.net/bug.php?id=69391&edit=1

  ID: 69391
  Updated by: rasmus@php.net
  Reported by: testing1932@gmail.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@gmail.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@gmail.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@gmail.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.


------------------------------------------------------------------------

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 5 of 5 | next ›
Discussion Overview
groupphp-bugs @
categoriesphp
postedApr 7, '15 at 6:59a
activeApr 7, '15 at 7:35a
posts5
users3
websitephp.net

People

Translate

site design / logo © 2017 Grokbase