#46246 [Opn->Asn]: difference between call_user_func(array($this, $method)) and $this->$method()

From: Date: Tue, 07 Oct 2008 17:01:29 +0000
Subject: #46246 [Opn->Asn]: difference between call_user_func(array($this, $method)) and $this->$method()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-129544@lists.php.net to get a copy of this message
 ID:               46246
 Updated by:       dmitry@php.net
 Reported By:      borysf at wp dot pl
-Status:           Open
+Status:           Assigned
 Bug Type:         Scripting Engine problem
 Operating System: Linux
 PHP Version:      5.2.6
-Assigned To:      
+Assigned To:      dmiry


Previous Comments:
------------------------------------------------------------------------

[2008-10-07 04:58:40] 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-06 23:26:43] 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-06 21:51:30] 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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=46246&edit=1



Thread (6 messages)

« previous php.bugs (#129544) next »