FAQ
Hi,

Why is func_num_args not working on closures?
Example code (tested on PHP 5.3.8):

<?php

class Test
{
public function testClosure($oClosure)
{
echo '<pre>';
print_r(func_num_args($oClosure));
echo '<pre>';
}
}

$c = new Test();
$c->testClosure(function($arg1, $arg2){
});

That returns 1 (the num of args in which function the closure is
(testClosure), but not the number of args from the closure itself.
Now Reflection can be used to get the number of args but that is overkill!
Also when you would do print_r(func_get_args($oClosure)); you do get
"something" back including the arguments but no way (that i know) to use
them!

Just something that i wonder...
I would have expected num args to return the args of the function you
provide it..

Kind regards,
Mark

Search Discussions

  • Rafael Kassner at Dec 20, 2011 at 11:54 pm
    func_num_args() must be called inside the Closure to know the number
    of arguments that it received. If you are calling outside, it returns
    the number of arguments of your testClosure function. There is no
    other way than Reflection to do this.

    And, I think func_num_args() does not receive any parameter.
    On Tue, Dec 20, 2011 at 9:43 PM, Mark wrote:
    Hi,

    Why is func_num_args not working on closures?
    Example code (tested on PHP 5.3.8):

    <?php

    class Test
    {
    public function testClosure($oClosure)
    {
    echo '<pre>';
    print_r(func_num_args($oClosure));
    echo '<pre>';
    }
    }

    $c = new Test();
    $c->testClosure(function($arg1, $arg2){
    });

    That returns 1 (the num of args in which function the closure is
    (testClosure), but not the number of args from the closure itself.
    Now Reflection can be used to get the number of args but that is overkill!
    Also when you would do print_r(func_get_args($oClosure)); you do get
    "something" back including the arguments but no way (that i know) to use
    them!

    Just something that i wonder...
    I would have expected num args to return the args of the function you
    provide it..

    Kind regards,
    Mark


    --
    Atenciosamente,
    Rafael Kassner
  • Gergo Erdosi at Dec 20, 2011 at 11:55 pm
    Hi,

    The function func_num_args() doesn't accept any parameters, so calling func_num_args($oClosure) is the same as calling simple func_num_args(). That's why it returns 1 instead of 2, which is I think the expected and correct behavior.

    Gergo Erdosi

    On Dec 21, 2011, at 24:43 , Mark wrote:

    Hi,

    Why is func_num_args not working on closures?
    Example code (tested on PHP 5.3.8):

    <?php

    class Test
    {
    public function testClosure($oClosure)
    {
    echo '<pre>';
    print_r(func_num_args($oClosure));
    echo '<pre>';
    }
    }

    $c = new Test();
    $c->testClosure(function($arg1, $arg2){
    });

    That returns 1 (the num of args in which function the closure is
    (testClosure), but not the number of args from the closure itself.
    Now Reflection can be used to get the number of args but that is overkill!
    Also when you would do print_r(func_get_args($oClosure)); you do get
    "something" back including the arguments but no way (that i know) to use
    them!

    Just something that i wonder...
    I would have expected num args to return the args of the function you
    provide it..

    Kind regards,
    Mark
  • Rafael Kassner at Dec 21, 2011 at 12:00 am
    My bad, replied only to sender. Fixing it now.

    Well, any time you need to know signature or another information about
    a function or class method, you should use Reflection. func_num_args()
    is useful for when you need a dynamic function, like this (an
    hypothetic example using func_get_args(), but you can get the point).

    function delete ()
    {
    $ids = func_get_args();
    // implementation
    }

    $success = delete(1, 2, 7);
    On Tue, Dec 20, 2011 at 9:54 PM, Mark wrote:
    Then there is no point for the func_num_args() function to accept a
    parameter at all if it takes the current function anyway.
    I don't see this as a feature, more like a bug!

    On Wed, Dec 21, 2011 at 12:51 AM, Rafael Kassner wrote:

    func_num_args() must be called inside the Closure to know the number of
    arguments that it received. If you are calling outside, it returns the
    number of arguments of your testClosude function. There is no other way than
    Reflection to do this.

    And, I thunk func_num_args() does not receive any parameter.
    On Dec 20, 2011, at 9:43 PM, Mark wrote:

    Hi,

    Why is func_num_args not working on closures?
    Example code (tested on PHP 5.3.8):

    <?php

    class Test
    {
    public function testClosure($oClosure)
    {
    echo '<pre>';
    print_r(func_num_args($oClosure));
    echo '<pre>';
    }
    }

    $c = new Test();
    $c->testClosure(function($arg1, $arg2){
    });

    That returns 1 (the num of args in which function the closure is
    (testClosure), but not the number of args from the closure itself.
    Now Reflection can be used to get the number of args but that is
    overkill!
    Also when you would do print_r(func_get_args($oClosure)); you do get
    "something" back including the arguments but no way (that i know) to use
    them!

    Just something that i wonder...
    I would have expected num args to return the args of the function you
    provide it..

    Kind regards,
    Mark


    --
    Atenciosamente,
    Rafael Kassner

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedDec 20, '11 at 11:43p
activeDec 21, '11 at 12:00a
posts4
users3
websitephp.net

People

Translate

site design / logo © 2022 Grokbase