php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46246 difference between call_user_func(array($this, $method)) and $this->$method()
Submitted: 2008-10-06 21:51 UTC Modified: 2008-10-10 15:55 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: borysf at wp dot pl Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-06 23:26 UTC] colder@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows (zip):
 
  http://snaps.php.net/win32/php5.2-win32-latest.zip

For Windows (installer):

  http://snaps.php.net/win32/php5.2-win32-installer-latest.msi

Can't reproduce on 5.2.6 and 5.2.7-dev.
 [2008-10-07 04:58 UTC] borysf at wp dot pl
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');
 [2008-10-10 15:55 UTC] dmitry@php.net
Your expectation is wrong. Calling Test from the child class means that private method can be overridden, however it can't.
 [2008-10-10 15:55 UTC] dmitry@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 06:01:30 2024 UTC