0

In PHP, is it possible to get the coded parameter count?

Something like this(pseudo code):

function($a=false , $b=true){ //the $a=true... is just parameters, with default values
  echo func_num_args();
  echo ", ";
  echo func_get_coded_params(); //how do i do this in PHP?
}

echo function(1 , 2); // output -> 2, 2 

echo function(1, 2, 3, 4, 5, a, b, c); // output -> 8, 2

Is there an way/imaginary function(func_get_coded_params()) that can retrieve the default coded parameter count? In this case, the coded parameters would be $a and $b itself which returns 2. If i do function($a, $b, $c) it would return 3

6
  • Why would you want to do this? It's constant in every function. Commented Aug 17, 2012 at 15:53
  • You shouldn't have $a and $b there if you using func_num_args() or func_get_args() Commented Aug 17, 2012 at 15:53
  • i still need my default parameters in my function, so that's why i need $a and $b and vars like that Commented Aug 17, 2012 at 15:55
  • I'm still not quite sure why you would need such a function, as it is a constant in a function. Commented Aug 17, 2012 at 15:59
  • Could you give example with outputs like 2, 1 or 3, 0 ? Commented Aug 17, 2012 at 15:59

3 Answers 3

4

Use ReflectionFunction and call the getNumberOfParameters method.

You can create an instance of the current function using:

$func = new ReflectionFunction(__FUNCTION__);

And get the number of defined parameters using

$func->getNumberOfRequiredParameters();

Edit

For use in classes.

$func = new ReflectionMethod(__CLASS__, __FUNCTION__);
Sign up to request clarification or add additional context in comments.

2 Comments

wait, what if i want to do this in __construct()? i tried to put this in construct and an exception is raised saying __construct() is not found...
For inside a class you will need to use ReflectionMethod instead. new ReflectionMethod(__CLASS__, __FUNCTION__);
2

PHP has a number of useful Reflection classes you can use.

function whatever($a, $b, $c)
{
  $reflection = new ReflectionFunction(__FUNCTION__);
  echo func_num_args() . ', ' .  $reflection->getNumberOfRequiredParameters();
}

whatever(1,2,3,4,5); // Prints 5, 3

Check http://www.php.net/manual/fr/class.reflectionfunction.php

Comments

0

This will do what you need:

class x
{
    function __construct($a=1,$b=2)
    {

        $constructor = new ReflectionMethod(__CLASS__,__FUNCTION__);
        $params = $func->getNumberOfParameters();
        echo func_num_args().",".$params;
    }
}
$x = new x(1,2,3,4); //output -> 4,2

Note that since you are having default values defined for your arguments in the constructor, what you need to call is getNumberOfParameters() and not getNumberOfRequiredParameters() (because there are no required parameters, they all have default values)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.