views:

251

answers:

4

Hi,

Is that possible to have a single PHP SOAP server which will handle requests to several classes (services)? If yes, could you please show an example implementation? If not, could you please describe why?

Thanks in advance, Artem.

+1  A: 

Could you wrap the other services in a single class? Completely untested, it was just a thought...

class MySoapService
{
  public function __construct()
  {
     $this->_service1 = new Service1();
     $this->_service2 = new Service2();
  }

  // You could probably use __call() here and intercept any calls, 
  //  thus avoiding the need for these declarations in the wrapper class...

  public function add($a, $b)
  {
     return $this->_service1->add($a, $b);
  }

  public function sub($a, $b)
  {
    return $this->_service2->sub($a, $b);
  }
}

class Service1
{
  public function add($a, $b)
  {
    return $a + $b;
  }
}

class Service2
{
  public function sub($a, $b)
  {
    return $a - $b;
  }
}
Keith Palmer
A: 

Thanks for reply.

And what if there are 2 methods in different classes with the same name?

hartem
A: 

Another take on the same general idea (proxy class) - for php5 Uses a hash to map functions to callbacks.

class ServiceProxy {
    private $map = array();

    public function addMethod($name, $callback) {
        if(is_callable($callback)) {
            $this->map[$name] = $callback;
            return true;
        }
        return false;
    }      

    function __call($name, $args) {
        if(isset($map[$name])) {
            return call_user_func_array($map[$name], $args);
        } else {
            return null;
        }
    }
}

This class could also use the reflection API and add all pulic methods from an object, for example.

gnud
A: 

If both classes had methods with identical names (but different parameters) then you could use func_get_args() to analyze the arguments and differentiate between the methods that way.

If they take the same arguments... then you're kinda stuck.

Why can't you just use two separate Web Services?

Keith Palmer