tags:

views:

47

answers:

2

Hey, I got a class like this (this is an abstract example):

class Example(){
    function test($param1,$param2,$param3=NULL){ //param3 is optional
        //stuff
    }
}

This function is called dynamically (from URL, /example/test/a/b/ will call Example::Test(a,b) )

if(is_callable(array($class, $funcname)))
   call_user_func_array(array($class, $funcname),$params);

/example/test/ is not allowed, because of missing parameters (will return 404)

/example/test/a/ is also not allowed

/example/test/a/b/ is allowed since $param3 is optional

/example/test/a/b/c/ is allowed

BUT:

/example/test/a/b/c/d/ will also call the function, because PHP just ignores the 4th paramater.

Since the func is called dynamically the script does not know how many parameters are allowed before calling the function. But I need the script to return a 404 error if there are too many parameters. I could not find a way to find the max. amount of parameters out before actually calling the function.

This one works:

    function test($param1,$param2,$param3=NULL,$error=NULL){ //param3 is optional;
        if(isset($error))$this->getErrorController()->notFoundHandler(); //will throw a 404 if $error is set and stop the script.
        //stuff
    }

Is there a better way to do this?

Thanks.

+3  A: 

Hello, you can check the return value of func_num_args() inside your function.

greg0ire
English URL: http://php.net/manual/en/function.func-num-args.php
chigley
whoops, fixed, thank you
greg0ire
+1  A: 

I found exactly what I was looking for. It's ReflectorClass (PHP5).

Max number of parameters:

http://www.php.net/manual/en/reflectionfunctionabstract.getnumberofparameters.php

Min number of parameters:

http://www.php.net/manual/en/reflectionfunctionabstract.getnumberofrequiredparameters.php

Eliasdx