# 5.3.8, Why is func_num_args() not working on a Clusure?

- Thread: https://externals.io/message/56969
- Original archive: https://news-web.php.net/php.internals/56969
- Started by Mark on 2011-12-20T23:43:13+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #56969 — Mark — 2011-12-20T23:43:13+00:00

URL: https://externals.io/message/56969#56969

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

---

## #56970 — Rafael Kassner — 2011-12-20T23:54:30+00:00

URL: https://externals.io/message/56969#56970 · In reply to: #56969

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.


> 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

---

## #56971 — Gergo Erdosi — 2011-12-20T23:55:50+00:00

URL: https://externals.io/message/56969#56971 · In reply to: #56969

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




> 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

---

## #56972 — Rafael Kassner — 2011-12-21T00:00:03+00:00

URL: https://externals.io/message/56969#56972

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);


> 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!
>
>
> 
> > […]
>



-- 
Atenciosamente,
Rafael Kassner
