php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52900 Protected instance method called by static method of the same class
Submitted: 2010-09-21 16:02 UTC Modified: 2010-09-21 21:58 UTC
From: e dot visciola at xaos dot it Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: Irrelevant OS: WIN & OSX
Private report: No CVE-ID: None
 [2010-09-21 16:02 UTC] e dot visciola at xaos dot it
Description:
------------
When I call a protected instance method of an object inside a class method of "the same type" I'd like to have an error.
The example explain:

Test script:
---------------
class A {
        
   protected function instanceMethod()
   {
   	echo 'Instance method called, but it is not right because instance method is protected<br />';
   }

    public static function staticMethod()
    {
   	echo 'Static method called<br />';
       	$a = new A();
       	$a->instanceMethod();
    }       
}

A::staticMethod();

Expected result:
----------------
That was generated an error to access the protected method

Actual result:
--------------
Static method called
Instance method called, but it is not right because instance method is protected

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-21 17:09 UTC] cataphract@php.net
-Status: Open +Status: Bogus
 [2010-09-21 17:09 UTC] cataphract@php.net
Not a bug.

Inside the static method, the calling scope is A. The scope of instanceMethod is also A.

Protected methods are accessible whenever their scope and the calling scope are in the same class hierarchy.
 [2010-09-21 17:43 UTC] e dot visciola at xaos dot it
OK, thanks...
I try also in Java and the behaviour is the same.
I'm sorry to have submit it.
Good work
 [2010-09-21 21:58 UTC] cataphract@php.net
Java actually has more complicated semantics for protected: see http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2 :

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object. [Inside the same package, access is permitted]

6.6.2.1 Access to a protected Member
Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:

* If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S. 
* If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Feb 23 18:01:28 2024 UTC