PHP get # of required parameters for function/method

The answer to this question may very well be no, and it may sound like an odd question to ask, but I have a need.

I am creating a class. This class may call another class that is optional. In the end, it will be up to the developer that uses my class to create the optional class. This optional class has some requirements. These requirements are that it has exactly two methods that can be called, that these two methods have specific names, and that each of these methods has a single parameter. What I want to do is to test to see if the class is available and meets the requirement. If it does then turn on the use of the optional class, if not then do not turn on the use of it.

I can test if the Class exists with: class_exists($className);
and I can test for each of the methods with: method_exists($className, $methodName);

Is there a way to test to see how many parameters are required by each methods? I would also need to take into account for the fact that the methods might have optional parameters, and these would not be a factor.

Any ideas are welcome.

The only thing that I can come up with so far is to read in the file for the class and use a regular expression to test the content of the file to find the methods and the number of arguments. I'd rather avoid this as I don't want the overhead of the file read each time the class in instantiated.

In the end I may simply have to go with testing to see if the methods exists and let the next developer worry about the errors if they do not create them properly, but I thought is was worth a shot to see if anyone knows another solution.
LVL 18
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

i'm sorry, i looked through manual, and it seems you can't achieve this with simple functions...
Maybe this will help you:

__call() is triggered when invoking inaccessible methods in an object context.
public function __call($name, $arguments) {

Open in new window

Hube02Author Commented:
@szewkam, I doubted I would be able to do this with a simple function.

@webvogel, Unfortunately I'm stuck in PHP 4 land. My applications must work cross platform and since 50% of my potential clients/customers are still using PHP 4 I can't use anything that is PHP 5 specific. Sorry, I should have stated that up front.

I'm planning to leave this open for a while. I know that I can read the file and use a regular expression to find what I'm looking for, I was just hoping for something a little classier. And like I said, I'll likely

I'm surprised that there is no way to get the content of the function itself. Take JavaScript for example. If you create the following function in JavaScript:

function doSomething() {
  // do something here

and then you set a variable equal to the function name:

var someVariable = doSomething;

then the variable is equal to the entire text of the function and you can actually parse the functions just like a string. This would be preferable to reading an entire file where the size of the file can be an issue.
Hube02Author Commented:
So, I became obsessed with finding workable solution.

You have to remember that I'm working in PHP4, but here is my solution to the problem.

This actually works quite well since any errors calling the functions or that occur within the functions will all trigger an error that will disable the optional class. This simply means that the class needs to be error free.

I'm posting this because I believe it may be of use to others. This is a much cut down version of my script, but all the necessities are there.

I will except any useful suggestions for improvements.
  // variable that will hold value indicating if an error occurs calling funtions of optional class
  $optional_class_error = false;
  // error handler that will be used to detect errors calling functions of optional class
  function optionalClassErrorHandler($errno, $errstr) {
    // an error occurred, set error to true
    global $optional_class_error;
    $optional_class_error = true;
  } // end function optionalClassErrorHandler
  // the main class
  class mainClass {
    var $optional_class_enable;
    // instantiation function (PHP4)
    function mainClass() {
      global $optional_class_error;
      $optional_class_enable = false;
      // check that class exists
      if (class_exists($optional_class)) {
        $this->optional_object = new $optional_class();
        // check that our methods exist
        if (method_exists($optional_class, 'Function1') &&
            method_exists($optional_class, 'Function2')) {
          // see if display_errors is turned on,
          // if yes then turn display errors off
          $display_errors = ini_get('display_errors');
          if ($display_errors) {
            ini_set('display_errors', '0');
          // set error reporting to desired level 
          // and store original value
          $error_reporting = error_reporting(E_ALL);
          // set the error handler
          // test each function to see if there is any errors
          // any errors calling the function or within the function
          // itself will trigger an error that will be handled by
          // the error handler
          $test_string = 'test_string';
          $test = $this->optional_object->Function1($test_string);
          if (!$optional_class_error) {
            $test2 = $this->optional_object->Function2($test_string);
            if (!$optional_class_error) {
              $this->optional_class_enable = true;
          // restore previous error handler
          // restore original error reporting
          // restore original value of display errors
          ini_set('display_errors', $display_errors);
    } // end function mainClass
  } // end class mainClass

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.