php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18926 cannot call self::method() or parent::method() via call_user_func_array()
Submitted: 2002-08-15 17:10 UTC Modified: 2003-06-09 05:55 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:1 of 2 (50.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: carl at thep dot lu dot se Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.0-dev OS: Linux
Private report: No CVE-ID: None
 [2002-08-15 17:10 UTC] carl at thep dot lu dot se
Using call_user_func_array() inside a class function produces different results from the equivalent direct function calls.
call_user_func_array(array('self,'method'), $args) gives an error, although self::method() works, as does call_user_func_array(array('myclassname','method'), $args).
(This format for calling static class methods is undocumented, by the way - is it officially even supposed to work?)

Also, in PHP 4.3-dev, a call to myfunction() inside a class function will check the current class's function table first, before calling a globally defined function. But call_user_func_array('myfunction',$args) does not - it only looks for global functions.

Whether any of these things are bugs or features, I don't know.

test script:

<?php
class foo
{
    function myname() { return 'foo'; }
    function myself()
    {
        $args = func_get_args();
        echo '<li>', self::myname(), '::myself - my args are ',var_export($args,TRUE),"\n";
    }
}
class bar extends foo
{
    function myname() { return 'bar'; }
    function test()
    {
        foo::myself('calling foo::myself');
        bar::myself('calling bar::myself');
        self::myself('calling self::myself');
        parent::myself('calling parent::myself');
        call_user_func_array(array('foo','myself'), array('call_user_func_array on array("foo","myself")'));
        call_user_func_array(array('bar','myself'), array('call_user_func_array on array("bar","myself")'));
        call_user_func_array(array('parent','myself'), array('call_user_func_array on array("parent","myself")'));
        call_user_func_array(array('self','myself'), array('call_user_func_array on array("self","myself")'));
        myself('calling myself');
        call_user_func_array('myself', array('call_user_func_array on "myself"'));
    }
}
function myself()
{
    echo '<li>i am the external function myself - my args are ', var_export(func_get_args(), TRUE), "\n";
}
bar::test('testing',1,2);
?>

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-08-15 20:09 UTC] kalowsky@php.net
Is this with ZE2 or ZE1?
 [2002-08-15 20:26 UTC] tater at potatoe dot com
You see the same behavior with PHP 4.3.0-dev and ZE1.3 for 'parent'. Calls to 'self' and the change in function name scope are ZE2 only features anyway (I think).
 [2002-08-15 20:40 UTC] kalowsky@php.net
can you please give a ZE example?
 [2002-08-15 21:09 UTC] tater at potatoe dot com
presuming ZE == ZE1.3.0, this is on a vanilla build from CVS
(just checked it out, ran buildconf, then configure w/o options):
<?php
class foo { function from_foo($p) { echo "i am from foo: $p\n"; } }
class bar extends foo {
    function test() {
        foo::from_foo('foo::from_foo');
        call_user_func(
            array('foo','from_foo')
            ,'call_user_func(array(foo,from_foo))'
        );
        parent::from_foo('parent::from_foo');
        call_user_func(
            array('parent','from_foo')
            ,'call_user_func(array(parent,from_foo))'
        );
    }
}
error_reporting(E_ALL);
bar::test();
?>
gives you:
# sapi/cli/php -v
PHP 4.3.0-dev (cli), Copyright (c) 1997-2002 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies
# sapi/cli/php ctest.php
i am from foo: foo::from_foo
i am from foo: call_user_func(array(foo,from_foo))
i am from foo: parent::from_foo

Warning: call_user_func(): First argumented is expected to be a valid callback, 'parent::from_foo' was given in ctest.php on line 14
 [2002-10-28 14:10 UTC] sterling@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2002-10-28 15:46 UTC] tater at potatoe dot com
If this is not a bug, then how exactly is one supposed to be able to make the function call

self::method($a,$b,$etc)

using call_user_func_array()??? or is that not supported? because this still doesn't work. a word or two of explanation would be polite.
 [2002-10-28 15:48 UTC] tater at potatoe dot com
Still doesn't work on ZE2.
 [2002-10-29 19:16 UTC] tater at potatoe dot com
Wrong summary, cut & paste error.
 [2003-02-09 09:57 UTC] moriyoshi@php.net
Related to bug #21849

 [2003-06-09 05:55 UTC] stas@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, 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/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 [2004-09-29 04:49 UTC] sibaz at sibaz dot com
The convention for this is to use array(&$obj, "method") instead of $function_name.  So:-
class foo
{
   function one()
   {
      echo "one";
   }
   function two()
   {
      $args = func_get_args();
      call_user_func_array(array(&$this, "one"), $args);
   }
};

$foo = new foo();
$foo->two();

Thats the equivalent to $this->one(); within $this->two();
The question is how do you inherit foo and call parent::two(); from within bar (thus calling foo->two(); Not bar->two(); )
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Aug 17 09:01:28 2024 UTC