note 42505 added to function.wddx-deserialize
| From: | xavieratc-est-simpledotcom at rn2 dot php dot net | Date: | Wed, 19 May 2004 08:09:55 +0000 |
| Subject: | note 42505 added to function.wddx-deserialize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69818@lists.php.net to get a copy of this message | ||
JavaScript/PHP interroperability:
If you want to exchange wddx packets between a JavaScript application using the JS wddx library from
openwddx (http://www.openwddx.org/downloads/), and a PHP script using wddx functions, be aware that
the encoding of the classes of objects serialized in the wddx packet is not made the same way in
both implementations.
The JS lib uses a 'type' attribute in the 'struct' tab and gives it the name of
the class of the object serialized in the 'struct' element, while PHP uses a
'php_class_name' tag within the 'struct'.
NB : to get the type attribute with JS serialisation, you need to set the
'wddxSerializationType' member in your classes
Example with an object of class 'exemple' containing 2 members _attr1 and _attr2.
(NB : in french, 'example' is spelled 'exemple', okay ? :-)
JS serialization :
<wddxPacket version='1.0'>
<header/>
<data>
<struct type="exemple">
<var name='_attr1'>
<string>foo</string>
</var>
<var name='_attr2'>
<string>bar</string>
</var>
</struct>
</data>
</wddxPacket>
php serialization:
<wddxPacket version='1.0'>
<header/>
<data>
<struct>
<var name='php_class_name'>
<string>exemple</string>
</var>
<var name='_attr1'>
<string>foo</string>
</var>
<var name='_attr2'>
<string>bar</string>
</var>
</struct>
</data>
</wddxPacket>
I like the JS approach better.
So if you need the compatibility, the workaround is quite simple:
- add a 'php_class_name' member in your JS objects, giving it the name of the class for
value, and the JS generated packet will be correctly deserialized with PHP in classed objects.
- use a regexp on a PHP generated packet to add the 'type' attribute so that the JS
deserialization instanciates objects of the right class:
$pat = "/<struct><var
name='php_class_name'><string>(\w+)<\/string>/" ;
$rep = "<struct type='\$1'><var
name='php_class_name'><string>\$1</string>" ;
$serialised = preg_replace($pat, $rep, $serialised );
NB : this only works because the 'php_class_name' tag is just next to the
'struct' tag.
Who knows if this is going to be true in future releases... ?
This way, you can exchange JS and PHP objects (provided you define your JS and PHP classes the same
way) smoothly...
It's worth saying that both implementations, as well as my interroperability workaround, handle
correctly objects with members that are also objects.
NB : NB means Nota Bene.
----
Manual Page -- http://www.php.net/manual/en/function.wddx-deserialize.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42505
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42505&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42505&report=yes
Search -- http://master.php.net/manage/user-notes.php