|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 04:00:02 2025 UTC |
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.