php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38992 invoke() and invokeArgs() static method calls should match
Submitted: 2006-09-29 13:15 UTC Modified: 2016-08-07 23:55 UTC
Votes:8
Avg. Score:4.1 ± 0.9
Reproduced:7 of 7 (100.0%)
Same Version:2 (28.6%)
Same OS:1 (14.3%)
From: matthew at zend dot com Assigned: cmb
Status: Closed Package: Reflection related
PHP Version: 5.1.6 OS: Debian SID on i686
Private report: No CVE-ID:
 [2006-09-29 13:15 UTC] matthew at zend dot com
Description:
------------
ReflectionMethod::invoke() and ReflectionMethod::invokeArgs() implementations currently do not support the same functionality.

Currently, ReflectionMethod::invoke() can be called using a string class name as the first argument *if* the method is declared static. However, ReflectionMethod::invokeArgs(), called the same way, raises a warning and does not invoke the method:

    Warning: ReflectionMethod::invokeArgs() expects parameter 1 to be object, string given

Calling with a string class name is undocumented currently, but a useful feature to have. I'd request that invokeArgs() be made to match the current invoke() functionality, and the documentation updated to indicate this usage.

Reproduce code:
---------------
<?php
class MyClass
{
    public static function doSomething()
    {
        echo "Did it!\n";
    }
}

$r = new ReflectionMethod('MyClass', 'doSomething');
$args = array();
$r->invoke('MyClass', array());
$r->invokeArgs('MyClass', $args);


Expected result:
----------------
Did it!
Did it!

Actual result:
--------------
Did it!

Warning: ReflectionMethod::invokeArgs() expects parameter 1 to be object, string given in ... line 13

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-28 19:37 UTC] andrea at 3site dot it
I suppose this isn't a bug since PHP 5 inherits classes public static methods too (and they call them ... feature ... ).

$r->invoke(new MyClass, array());

This should be expected bahaviour or there's something wrong on this "bogus": http://bugs.php.net/bug.php?id=40886

If this bug will be solved, PHP developers should think about *difference* between public static methods and instances methods, that are two different things, expecially without overload possibility changing arguments or using __call too, that in this case, isn't "so magic", IMHO.
 [2010-12-20 14:17 UTC] jani@php.net
-Package: Feature/Change Request +Package: Reflection related
 [2011-05-11 22:53 UTC] kaplich at gmail dot com
If anybody needs invokeArgs functionality though the following code works fine:

<?
class A
{
	public static function run()
	{
		$class = new ReflectionClass('B');
		$method = $class->getMethod('foo');
		call_user_func_array(array($method, 'invoke'), array('B', 1, 2));
	}
}

class B extends A
{
	public static function foo($v1, $v2)
	{
		echo $v1." / ".$v2;
	}
}

B::run();

Expected result:
----------------
1 / 2

Actual result:
--------------
1 / 2
 [2016-08-07 18:31 UTC] cmb@php.net
-Status: Assigned +Status: Analyzed -Assigned To: johannes +Assigned To: cmb
 [2016-08-07 18:31 UTC] cmb@php.net
> Currently, ReflectionMethod::invoke() can be called using a
> string class name as the first argument *if* the method is
> declared static.

Actually, the first parameter to ReflectionMethod::invoke() is
completely ignored, if the method is a static method, see
<https://3v4l.org/e9h1S>. A comment in the sources[1] also
explains the reasoning.

This doesn't make any sense, however, because for static methods
to be invoked, NULL is supposed to be passed as first parameter to
::invoke() as well as ::invokeArgs(). Allowing an arbitrary
argument is confusing at best.

Changing the behavior would introduce a BC break, though, so it
appears to be reasonable to do that for PHP 7.1 only.

[1] <https://github.com/php/php-src/blob/PHP-7.0.10/ext/reflection/php_reflection.c#L3197-L3202>
 [2016-08-07 23:07 UTC] cmb@php.net
-Summary: ReflectionMethod::invoke() and ::invokeArgs() static method calls should match +Summary: invoke() and invokeArgs() static method calls should match
 [2016-08-07 23:55 UTC] cmb@php.net
-Status: Analyzed +Status: Closed
 [2016-08-07 23:55 UTC] cmb@php.net
This request has been implemented with
<http://git.php.net/?p=php-src.git;a=commit;h=f706897f330038d17570f5d0e6091e06d90179a6>.
 
PHP Copyright © 2001-2016 The PHP Group
All rights reserved.
Last updated: Sun Sep 25 05:01:38 2016 UTC