你不能這樣做SOAP
。基本上,您的PHP類正在映射到由XML模式定義的XML數據結構。該映射只包含屬性,不能包含可執行代碼。 SOAP
是專爲互操作性而設計的,當然,您不能在PHP和Java或.NET之間共享代碼。在接收端,您的XML數據結構正在轉換爲客戶端編程語言的數據結構(如果您使用SoapClient
或C#
類,如果使用C#
,則爲PHP類)。由於XML數據結構僅包含屬性信息,因此不能重建始發類的可執行部分。
但是如果SOAP服務器和連接客戶端都可以訪問相同的代碼庫(這意味着相同的類),那麼有一件事可以提供幫助。您可以使用classmap
選項在SoapClient
的構造函數中定義XML
類型和PHP
類之間的映射。這允許SoapClient
將傳入的XML數據結構映射到真正的PHP類 - 考慮到服務器和客戶端都可以訪問相關的類定義。這使您可以使用SOAP
通信接收端的方法。
class book {
public $a = "a";
public $b = "c";
public function getName() {
return $this->a.' '.$this->b;
}
}
$options = array(
'classmap' => array('book' => 'book')
);
$client = new SoapClient('path/to/wsdl', $options);
$book = $client->test();
echo $book->getName();
的WSDL
可能看起來像(從SoapClient
測試的一個複製和adpated):
<wsdl:definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.nothing.com" targetNamespace="http://schemas.nothing.com">
<wsdl:types>
<xsd:schema targetNamespace="http://schemas.nothing.com">
<xsd:complexType name="book">
<xsd:all>
<xsd:element name="a" type="xsd:string"/>
<xsd:element name="b" type="xsd:string"/>
</xsd:all>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<message name="testRequest">
</message>
<message name="testResponse">
<part name="res" type="tns:book"/>
</message>
<portType name="testPortType">
<operation name="test">
<input message="tns:testRequest"/>
<output message="tns:testResponse"/>
</operation>
</portType>
<binding name="testBinding" type="tns:testPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="test">
<soap:operation soapAction="http://localhost:81/test/interface.php?class=test/dotest" style="rpc"/>
<input>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://schemas.nothing.com"/>
</input>
<output>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://schemas.nothing.com"/>
</output>
</operation>
</binding>
<service name="test">
<port name="testPort" binding="tns:testBinding">
<soap:address location="http://localhost:81/test/interface.php?class=test"/>
</port>
</service>
</wsdl:definitions>
The State of SOAP in PHP,如果你在PHP做SOAP
可能是有趣的。
噢謝謝你。你的溶劑很好,但不符合我的需求。我需要將這些方法隱藏起來。謝謝 – keepwalking
你想在客戶端使用方法,但想隱藏它們嗎?我不認爲這是行不通的... –
如果我在SoapServer中使用setClass,我可以使用隱藏方法:)無論如何,我可以通過返回一個客戶端soap對象來做一個解決方法。 – keepwalking