|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-10-06 21:51 UTC] borysf at wp dot pl
Description:
------------
There is a difference in behavior of call_user_func(array($this, $method)) and $this->$method() while class B inherits from class A and both declares the same method. The call_user_func() function seems to work as expected, but using $this->$method() seems to invoke parent method implementation instead of the overridden one.
Reproduce code:
---------------
class A
{
protected function Test()
{
echo 'Hello from '.get_class($this);
}
public function call($method, $args = array())
{
return $this->$method();
//return call_user_func(array($this, $method));
}
}
class B extends A
{
protected function Test()
{
echo 'Overridden hello from '.get_class($this);
}
}
$a = new A;
$b = new B;
$a->call('Test');
$b->call('Test');
Expected result:
----------------
Hello from A
Overridden hello from B
Actual result:
--------------
Hello from A
Hello from B
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 08:00:01 2025 UTC |
I made a mistake in the code snipped. Method Test in class A should be declared as private, then the problem occures. Here is the corrected code: class A { private function Test() { echo 'Hello from '.get_class($this); } public function call($method, $args = array()) { return $this->$method(); //return call_user_func(array($this, $method)); } } class B extends A { protected function Test() { echo 'Overridden hello from '.get_class($this); } } $a = new A; $b = new B; $a->call('Test'); $b->call('Test');