在 PHP 中,我想知道 SOAP 调用的方法是什么。这是一个示例以了解...
$soapserver = new SoapServer();
$soapserver->setClass('myClass');
$soapserver->handle();
我想知道的是handle()中会执行的方法名
谢谢!!
最佳答案
在我看来,在这种情况下访问被调用操作名称的最简洁、最优雅的方法是使用某种Wrapper 或Surrogate 设计模式。根据您的意图,您可以使用 Decorator或 Proxy .
举个例子,假设我们想动态地向我们的 Handler
对象添加一些额外的功能而不触及类本身。这允许保持 Handler
类更简洁,从而更专注于它的直接职责。这样的功能可以是记录方法及其参数或实现某种缓存机制。为此,我们将使用Decorator 设计模式。而不是这样做:
class MyHandlerClass
{
public function operation1($params)
{
// does some stuff here
}
public function operation2($params)
{
// does some other stuff here
}
}
$soapserver = new SoapServer(null, array('uri' => "http://test-uri/"));
$soapserver->setClass('MyHandlerClass');
$soapserver->handle();
我们将执行以下操作:
class MyHandlerClassDecorator
{
private $decorated = null;
public function __construct(MyHandlerClass $decorated)
{
$this->decorated = $decorated;
}
public function __call($method, $params)
{
// do something with the $method and $params
// then call the real $method
if (method_exists($this->decorated, $method)) {
return call_user_func_array(
array($this->decorated, $method), $params);
} else {
throw new BadMethodCallException();
}
}
}
$soapserver = new SoapServer(null, array('uri' => "http://test-uri/"));
$soapserver->setObject(new MyHandlerClassDecorator(new MyHandlerClass()));
$soapserver->handle();
如果您想控制对处理程序操作的访问,例如,为了强加访问权限,请使用代理设计模式。
关于PHP Soap Server如何知道被调用的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10705298/