PHP SoapClient类映射的优势
有人能告诉我在PHP Soapclient中使用classmap选项的优点吗?也许有一些实际的例子?classmap选项可用于将一些WSDL类型映射到PHP类 例如PHP SoapClient类映射的优势,php,soap,soap-client,Php,Soap,Soap Client,有人能告诉我在PHP Soapclient中使用classmap选项的优点吗?也许有一些实际的例子?classmap选项可用于将一些WSDL类型映射到PHP类 例如 class MyLoginResult { protected $serverUrl; protected $sessionId; public function getServerUrl() { return $this->serverUrl; } pub
class MyLoginResult {
protected $serverUrl;
protected $sessionId;
public function getServerUrl()
{
return $this->serverUrl;
}
public function getSessionId()
{
return $this->sessionId;
}
public function getServerInstance()
{
$match = preg_match(
'/https:\/\/(?<instance>[^-]+)\.example\.com/',
$this->serverUrl,
$matches
);
return $matches['instance'];
}
}
$client = new SoapClient("books.wsdl",
array('classmap' => array('LoginResult' => "MyLoginResult")));
$loginResult = $client->getLoginResult();
$instance = $loginResult->getServerInstance();
类MyLoginResult{
受保护的$serverUrl;
受保护的$sessionId;
公共函数getServerUrl()
{
返回$this->serverUrl;
}
公共函数getSessionId()
{
返回$this->sessionId;
}
公共函数getServerInstance()
{
$match=预匹配(
“/https:\/\/(?[^-]+)\.example\.com/”,
$this->serverUrl,
$matches
);
返回$matches['instance'];
}
}
$client=newsoapclient(“books.wsdl”,
数组('classmap'=>array('LoginResult'=>MyLoginResult”);
$loginResult=$client->getLoginResult();
$instance=$loginResult->getServerInstance();
除了Hoangtienan的评论之外,在使用映射类时,我还显示了另一个优势
例如,您可以通过一个方法扩展该类,当SoapClient将其数据传递给映射类时会触发该方法(您应该知道,如果您的属性是public,则不会触发该方法)
在这种情况下,您可以在将SoapClient传递的数据分配给数据类之前对其进行替换
类MyLoginResult{
受保护的$serverUrl;
受保护的$sessionId;
私人$已登录;
公共函数集($name,$value){
如果($name=='login\u status'){
$this->is_logged_in=($value='logged_in')?true:false;
}否则{
$this->$name=$value;
}
}
公共函数loginsucessfull(){
return$this->是否已登录;
}
//来自黄天南的类代码
}
e、 在这个例子中,我们从Soap获得一个字符串,但我们在类中存储一个bool值。
您可以将其用于其他更改,例如,如果您希望将内部变量存储在数组中,而不是使用直接属性