note 83410 added to ref.soap

From: Date: Sat, 24 May 2008 09:28:18 +0000
Subject: note 83410 added to ref.soap
Groups: php.notes 
Request: Send a blank email to php-notes+get-139690@lists.php.net to get a copy of this message
PROBLEM (with SOAP extension under PHP5) of transferring object, that contains objects or array of objects. Nested object would not transfer. SOLUTION: This class was developed by trial and error by me. So this 23 lines of code for most developers writing under PHP5 solves fate of using SOAP extension. <?php /* According to specific of organization process of SOAP class in PHP5, we must wrap up complex objects in SoapVar class. Otherwise objects would not be encoded properly and could not be loaded on remote SOAP handler. Function "getAsSoap" call for encoding object for transmission. After encoding it can be properly transmitted. */ abstract class SOAPable { public function getAsSOAP() { foreach($this as $key=>&$value) { $this->prepareSOAPrecursive($this->$key); } return $this; } private function prepareSOAPrecursive(&$element) { if(is_array($element)) { foreach($element as $key=>&$val) { $this->prepareSOAPrecursive($val); } $element=new SoapVar($element,SOAP_ENC_ARRAY); }elseif(is_object($element)) { if($element instanceof SOAPable) { $element->getAsSOAP(); } $element=new SoapVar($element,SOAP_ENC_OBJECT); } } } // ------------------------------------------ // ABSTRACT EXAMPLE // ------------------------------------------ class PersonList extends SOAPable { protected $ArrayOfPerson; // variable MUST be protected or public! } class Person extends SOAPable { //any data } $client=new SoapClient("test.wsdl", array( 'soap_version'=>SOAP_1_2, 'trace'=>1, 'classmap' => array('Person' => "Person", 'PersonList' => "PersonList") )); $PersonList=new PersonList; // some actions $PersonList->getAsSOAP(); $client->someMethod($PersonList); ?> So every class, which will transfer via SOAP, must be extends from class SOAPable. As you can see, in code above, function prepareSOAPrecursive search another nested objects in parent object or in arrays, and if does it, tries call function getAsSOAP() for preparation of nested objects, after that simply wrap up via SoapVar class. So in code before transmitting simply call $obj->getAsSOAP() ---- Server IP: 195.54.192.44 Probable Submitter: 127.0.0.1 ---- Manual Page -- http://www.php.net/manual/en/ref.soap.php Edit -- https://master.php.net/note/edit/83410 Del: integrated -- https://master.php.net/note/delete/83410/integrated Del: useless -- https://master.php.net/note/delete/83410/useless Del: bad code -- https://master.php.net/note/delete/83410/bad+code Del: spam -- https://master.php.net/note/delete/83410/spam Del: non-english -- https://master.php.net/note/delete/83410/non-english Del: in docs -- https://master.php.net/note/delete/83410/in+docs Del: other reasons-- https://master.php.net/note/delete/83410 Reject -- https://master.php.net/note/reject/83410 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139690) next »