Php WSDL和SOAP:返回带有方法的对象

Php WSDL和SOAP:返回带有方法的对象,php,soap,wsdl,Php,Soap,Wsdl,有没有办法在soap中返回一个对象及其方法?如果我在WSDL中返回xsd:struct,我只能获得对象的属性,但不能使用任何方法 比如说 class person { var $name = "My name"; public function getName() { return $this->name; } } 因此,在获取对象之后: $client = new SoapClient(); $person = $client->getPerson()

有没有办法在soap中返回一个对象及其方法?如果我在WSDL中返回xsd:struct,我只能获得对象的属性,但不能使用任何方法

比如说

class person
{
  var $name = "My name";
  public function getName()
  {
      return $this->name;
  }
}
因此,在获取对象之后:

$client = new SoapClient();
$person = $client->getPerson();
echo $person->getName(); // Return "My Name";

谢谢。

你不能用
SOAP
来做这件事。基本上,PHP类被映射到由XML模式定义的XML数据结构。此映射仅包含属性,不能包含可执行代码
SOAP
是为互操作性而设计的,自然不能在PHP和Java或.NET之间共享代码。在接收端,您的XML数据结构正在转换为客户机编程语言的数据结构(如果使用
SoapClient
则为PHP类,如果使用
C
则为
C
类)。由于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
测试中复制并应用):



如果你正在用PHP做
SOAP
,可能会很有趣。

哦,谢谢。你的解决方案很好,但不符合我的需要。我需要对公众隐瞒这些方法。谢谢你想在客户端使用方法,但想隐藏它们吗?我认为这根本不起作用……如果我在SoapServer中使用setClass,我可以使用隐藏方法:)无论如何,我可以通过返回客户端soap对象来解决问题。
<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>