php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43083 Protected method access from outside class.
Submitted: 2007-10-23 14:22 UTC Modified: 2007-10-23 16:09 UTC
From: purpurby at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.4 OS: RedHat
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: purpurby at gmail dot com
New email:
PHP Version: OS:

 

 [2007-10-23 14:22 UTC] purpurby at gmail dot com
Description:
------------
Availability to call protected method from outside class or its descendants.

Reproduce code:
---------------
<?php

class a
{
  protected function aa()
  {
    echo "a-aa\n";
  }
}

class b extends a
{
  protected function aa()
  {
    echo "b-aa\n";
  }  
}

// Extends is affects on the behaviour:
//   - When "c" extends the "a" - not correct behaviour: working as in "Actual result"
//   - When "c" NOT extends the "a" - correct behaviour: working as in "Expected result"
class c extends a 
{
  public function cc()
  {
    $obj = new a();
    $obj->aa();
    
    $obj2 = new b();
    $obj2->aa();
  }

}

$c = new c();
$c->cc();

?>

Expected result:
----------------
Fatal error: Call to protected method a::aa() from context 'c'

Actual result:
--------------
a-aa
b-aa

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-23 16:09 UTC] helly@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

First part is perfectly right. As binding happens at compile time only relevant part is that C is-a A, hence inside C calls to A\'s protected members are fine. The other thing is a bit odd since technically C and B are different things. However what you call is inside A and both have A as a common root, hence that is fine as well.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 04:01:38 2025 UTC