Reputation: 3025
I have a function which takes two arguments:
function index($LoadFunction, $footer) {
// My statements
}
I can get the total number of arguments sent to this function like this index(1,2,3,4)
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
What i want is to know how many arguments I am allowed to send to this index()
function.
How i will be able to do this.
EDIT1:
I tried these also, but this does not echo anything
https://stackoverflow.com/a/346789/1182021
https://stackoverflow.com/a/346943/1182021
EDIT2:
Ok, i'll explain it in more appropriate way:
class foo { function bar ( arg1, arg2 ){ ..... } }
I call class foo
like this:
$class = new foo(); // Instantiating my class $class->bar(1,2); // This piece of code is fine
Now what i want to do is:
$method = new ReflectionClass('foo', 'bar'); $num = $method->getNumberOfParameters(); // Ideally this should give me total number of arguments which this 'bar' // function can take, but it won't echo anything.
So how i can get this, I don't want to go into function to check it: I want to check it before executing the function.
How i can use this thing:
ReflectionFunctionAbstract::getNumberOfParameters — Gets number of parameters ReflectionFunctionAbstract::getNumberOfRequiredParameters — Gets number of required parameters
The above is from http://php.net/manual/en/book.reflection.php
Upvotes: 1
Views: 1839
Reputation: 10202
The number of arguments are virtually endless. Limitations depends not on the language itself, but on hardware/os limitations like memory usage and stuff like that. So you'll probably will not encounter a situation where it will really cause any problems.
You could also instead of and endless list of arguments just pass an array as an argument, which in itself can also hold virtually an endless list of key/value pairs. Will not make a big difference in terms of resource usage, but personally I find it easier to handle when the argument list is possibly really really long.
-- EDIT
If I understand correctly what you want to do (but please explain yourself more clearly if I don't), this is what you want:
function my_function($arg1, $arg2) {
if(func_num_args() > 2) {
throw new ErrorException('Maximum number of arguments exceeded');
// OR: trigger_error('Maximum number of arguments exceeded', E_USER_ERROR);
}
echo 'You are inside "my_function()" and have passed exactly 2 arguments';
}
When you execute this:
my_function('some value', 'another value');
it echoes: You are inside "my_function()" and have passed exactly 2 arguments
But when you execute this code:
my_function('some value', 'another value', 'too much arguments...');
It'll throw an exception (or trigger an error, whatever you choose)
-- Edit 2
Yes now I understand ;)
First of all: you try to use ReflectionClass on a method. You should use either ReflectionFunction (for a procedural function) or ReflectionMethod (for a class method).
Option 1:
function my_function($arg1, $arg2) {
// do something
}
$reflection = new ReflectionFunction('my_function');
echo 'Function "my_function" has '. $reflection->getNumberOfParameters() .' arguments';
Option 2:
class MyClass {
function my_function($arg1, $arg2) {
// do something
}
}
$reflection = new ReflectionMethod('MyClass::my_function');
echo 'Class method MyClass::my_function() has '. $reflection->getNumberOfParameters() .' arguments';
You can use $reflection->getNumberOfRequiredParameters()
instead in both cases
Upvotes: 1
Reputation: 56982
PHP has support for variable-length argument lists in user-defined functions. This is really quite easy, using the func_num_args(), func_get_arg(), and func_get_args() functions.
No special syntax is required, and argument lists may still be explicitly provided with function definitions and will behave as normal.
So you can pass any number of arguments to your function index(1,2,3,4,5,...)
In Your function if you are not passing at-least 2 parameters PHP generates a warning.
check http://php.net/manual/en/functions.arguments.php
Upvotes: 1