0

If I have the following class:

class MyClass
{
    public function hello($name)
    {
        echo 'Hello ' . $name;
    }
}

If I use the following code to call the function (the name of the function and parameters are determined based on user input):

$className = 'MyClass';
$func = 'hello';
$params = ['John', 'Another Param'];
$myClass = new $className();

$myClass->$func(...$params);

The function is called and the extra parameter is ignored. What I would like to do, is ensure that the number of parameters in $params matches that of the function and have and throw an ArgumentCountError if the number of arguments do not match.

I have been searching but have not found any solutions. How may I achieve this?

8
  • stackoverflow.com/questions/11480437/… Commented Aug 16, 2021 at 0:20
  • @segFault Thanks. Doing it that way would mean that I would have to do it for every function that I create which would not be the best idea. Commented Aug 16, 2021 at 0:33
  • Does the parameter will always be an array? Commented Aug 16, 2021 at 1:03
  • @MarcioMazzucato Yes because they are determined based on user input so the amount of arguments will not always be the same. Commented Aug 16, 2021 at 1:24
  • So you expect to receive always an array with an undefined number of items? Commented Aug 17, 2021 at 0:03

0

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.