PHP5: passing objects as a parameter in a SOAP call
Translation of the original in Catalan. Read it in: CatalàCastellano
Part 2 of 3SOAP web services with PHP5
- PHP5 - Generating WSDL with Zend Studio 5.1
- PHP5: passing objects as a parameter in a SOAP call
- PHP5: passing and returning objects as a parameter in a SOAP call (Part II)
Update: THE CONCLUSIONS OF THIS ARTICLE ARE WRONG SEE: PHP5: passing and returning objects as a parameter in a SOAP call (Part II)
Working on a project we're programming in PHP5 this doubt came up, especially after reading about the limitations of the SOAP servers PHP5 implements. The subject finally has a positive answer: YES, BUT.... Below I give you an example of how this works with PHP5 and what limitations it has.
We define the SOAP server which, as you can see, is very simple; the only thing the server does is re-send the same object it has received.
<php
class SOAPservice {
function peticion($per) {
return $per;
}
}
$server = new SoapServer("server.wsdl");
$server->setClass("SOAPservice");
$server->handle();
?>
Below you can see the client code, it's a very simple piece of code of only two lines. As we can see, first we define the persona class and before calling the SOAP client we instantiate the class, then we use that object as a parameter in the SOAP call. We capture the object the SOAP server returns and then we show its content with the print_r command.
<?php
/*
definim classe persona
*/
class persona {
public $nom;
function setNom($nom) {
$this->nom=$nom;
}
function setCognom($cognom) {
$this->cognom=$cognom;
}
function getNom() {
return $this->nom;
}
function getCognom() {
return $this->cognom;
}
}
/*
creem objecte tipus persona
*/
$myPersona=new persona;
$myPersona->setNom("el_nom");
$myPersona->setCognom("el_cognom");
/*
fem una crida SOAP amb un objecte com a parametre
*/
$client = new SoapClient("server.wsdl");
$myObj=$client->peticion($myPersona);
/*
mostrem objecte generic que retorna la crida SOAP
*/
print_r($myObj);
?>
If we look at the output we get when we call the client we can see that the object returned is not of type persona but of an internal PHP5 type called stdClass. That object only has the public attributes. We can't access the private or protected attributes and we've lost all the methods, whatever type they are.
stdClass Object
(
[nom] => el_nom
[cognom] => el_cognom
)
That's everything I've been able to achieve, it's not much but at least it was enough for me.
2 comments
Comments from the old blog, as they were written. Comments are closed.
javiVF
Sembla com si no estiguesis content amb el resultat, a mi em sembla correcte el funcionament de la POO que comentes.
atributs/metodes publics: visibles desde tots els llocs
atributs/metodes protegits: visibles desde la propia clase i les classes filles d'aquesta
atributs/metodes privats: nomes visbles desde la propia classe
Si vols traure mes informacio sobre la classe has de tirar ma de "reflection"
Tot aixo partint de que no he treballat mai amb objectes amb el PHP pero he pegat una ullada a la seva web ;)
—
La gràcia del tema esta, en que quan l'objecte arriba al servidor SOAP no té tipus, es converteix en un tipus standar com el que comento a l'article i jo em pensava que seguiria mantenint el tipus, malgrat hagués de tenir la classe declara en el servidor, però no és així. De fet, no passa res però hagués estat un puntasso poder passar tots els mètodes i propietats de cop d'un PC a un altre a través de SOAP.