php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40840 Derived class cant call its private method from the context of its base class
Submitted: 2007-03-16 23:25 UTC Modified: 2016-05-05 11:49 UTC
From: davcha at nordnet dot fr Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.2.1 OS: Windows XP SP2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: davcha at nordnet dot fr
New email:
PHP Version: OS:

 

 [2007-03-16 23:25 UTC] davcha at nordnet dot fr
Description:
------------
A derived class 'SubClass' cannot call a private method declared into 'SubClass' from the context of its parent class 'BaseClass'.

However, even if the private method is called from 'BaseClass', dumping the variable $this shows the current class type is 'SubClass'.

Reproduce code:
---------------
<?php
class BaseClass {
	public function A($f){
		var_dump($this);
		$this->$f();
	}
}

class SubClass extends BaseClass {
	private function B(){
		echo 'hello, i dont show !';
	}
}

$b = new SubClass();
$b->A('B');
?>

Expected result:
----------------
object(SubClass)#1 (0) { } hello, i dont show !

Actual result:
--------------
object(SubClass)#1 (0) { }
Fatal error: Call to private method SubClass::B() from context 'BaseClass' in C:\httpd\htdocs\pwidgets\test2.php on line 5


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-16 23:58 UTC] johannes@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

You are calling a method from SubClass out of the BaseClass context.
 [2007-03-17 00:22 UTC] davcha at nordnet dot fr
How about this :

<?php
class BaseClass {
	protected function A($f){
		var_dump($this);
		$this->$f();
	}
}

class SubClass extends BaseClass {
	private function B(){
		echo 'hello, i dont show !';
	}
	public function C(){
		$this->A('B');
	}
}

$b = new SubClass();
$b->C('B');
?>

In this case, i'm calling SubClass:B() from SubClass context. At least, that's how it should work : that's how it works in C, C#, etc...

In PHP 5.2.1 i'm still getting the same fatal error.
 [2016-05-05 11:49 UTC] nikic@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2016-05-05 11:49 UTC] nikic@php.net
The whole point of "private" is that access is only possible from within the class that declares the method (the usual loopholes using reflection and closure rebinding notwithstanding). This is definitely not going to change.

Also [citation needed] on your claim that other languages allow what you suggest, because I'm pretty sure they don't.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 18:01:28 2024 UTC