1

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?

1
  • Eliasdx, please upvote and accept answers. This makes folks more likely to help. Commented Oct 4, 2010 at 17:33

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

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.