1

I'm calling an objects method through call_user_func_array to which I pass dynamic string arguments depending on a couple of parameters.

It currently look similar to this:

<?php
class MyObject
{
     public function do_Procedure ($arg1 = "", $arg2 = "")
     { /* do whatever */ }


     public function do_Something_Else (AnotherObject $arg1 = null)
     { /* This method requires the first parameter to
          be an instance of AnotherObject and not String */ }
}

call_user_func_array(array($object, $method), $arguments);
?>

This works for method $method = 'do_Procedure' but if I wanted to call the $method = 'do_Something_Else' method which requires the first argument to be an instance of AnotherObject I get an E_RECOVERABLE_ERROR error.

How do I know which type of instance that should be passed? E.g. if this method requires an object instance but the first processed argument is of string, how do I recognize this so that I can pass null instead or simply skip the call?

3
  • 1
    Sounds like you're being way too dynamic in your function calls. You don't know what you're calling and you don't know what arguments you're passing... how's that gonna work for anything? Commented Sep 5, 2012 at 20:03
  • Well, I'm routing the request through a controller object and methods represent a call handle. Some call handle methods may take a POST or GET object wrapper and thus should only be of the correct instance. If string is passed you would get trying to call xxx on a none object. Commented Sep 5, 2012 at 20:07
  • I just want to validate the argument before the method instead of manually do the argument validation on each method. Commented Sep 5, 2012 at 20:09

1 Answer 1

2

$arguments is a array that wil explode to the parameters of the function. If you call the do_Something_Else function the array must either be empty or the first element must be null or a instance of AnotherObject

In all other situations you get a E_RECOVERABLE_ERROR error.

To find out what argument needs to be passed you can use the Reflectionclass

Sample, needs some work to adjust tou your needs:

  protected function Build( $type, $parameters = array( ) )
  {
    if ( $type instanceof \Closure )
      return call_user_func_array( $type, $parameters );

    $reflector = new \ReflectionClass( $type );

    if ( !$reflector->isInstantiable() )
      throw new \Exception( "Resolution target [$type] is not instantiable." );

    $constructor = $reflector->getConstructor();

    if ( is_null( $constructor ) )
      return new $type;

    if( count( $parameters ))
      $dependencies = $parameters; 
    else 
      $dependencies = $this->Dependencies( $constructor->getParameters() );

    return $reflector->newInstanceArgs( $dependencies );
  }

  protected static function Dependencies( $parameters )
  {
    $dependencies = array( );

    foreach ( $parameters as $parameter ) {
      $dependency = $parameter->getClass();

      if ( is_null( $dependency ) ) {
        throw new \Exception( "Unresolvable dependency resolving [$parameter]." );
      }

      $dependencies[] = $this->Resolve( $dependency->name );
    }

    return ( array ) $dependencies;
  }
Sign up to request clarification or add additional context in comments.

2 Comments

I was thinking of reflections to see the argument type. But I'm not sure it is possible, that is why I'm asking. And your answer is exactly what I explain in my question.
ReflectionParameter::getClass was exactly what I was looking for! Thank you.

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.