Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Decoding Data with WDDX - Page 5

September 4, 2002

Although there are five different functions available to encode data into WDDX, PHP has only a single function to perform the deserialization of WDDX packets. This function is named wddx_deserialize(), and it accepts a string containing a WDDX packet as its only argument.

Listing 5.14 demonstrates how a PHP variable encoded in WDDX can be deserialized by wddx_deserialize().

Listing 5.14 Deserializing a WDDX Packet into a Native PHP Structure

<?php

$flavor = "blueberry";

// print value before converting to WDDX
echo "Before serialization, \$flavor = $flavor <br>";

// serialize into WDDX packet
$packet = wddx_serialize_value($flavor);

// deserialize generated packet and display value
echo "After serialization, \$flavor = " . wddx_deserialize($packet);
?>

This works with arrays, too—in Listing 5.15, the deserialized result $output is an array containing the same elements as the original array $stooges.

Listing 5.15 Deserializing a WDDX Packet into a PHP Array

<?php

$stooges = array("larry", "curly", "moe");

// serialize into WDDX packet
$packet = wddx_serialize_value($stooges);

// deserialize generated packet
$output = wddx_deserialize($packet);

// view it
print_r($output);
?>

Buyer Beware!

There's an important caveat to keep in mind when using PHP's WDDX module. As shown in Listings 5.4 and 5.12, the wddx_serialize_vars() and wddx_add_vars() functions work in a slightly different manner from the wddx_serialize_value() function. Both wddx_serialize_vars() and wddx_add_vars() use WDDX structures (represented by <struct> elements) to store variables and their values, regardless of whether the variable is a string, number, or array. On the other hand, wddx_serialize_value() uses a <struct> only if the variable is an associative array.

This variation in the serialization process can significantly impact the deserialization process because PHP's wddx_deserialize() function automatically converts <struct>s into associative arrays. Consequently, the manner in which you access the original value of the variable will change, depending on how it was originally serialized.

Consider Table 5.1, which demonstrates the difference.

Table 5.1 A Comparison of Serialization with wddx_serialize_value() and wddx_serialize_vars()

wddx_serialize_value()

wddx_serialize_vars()

<?php

<?php

$lang = array("PHP", "Perl", "Python", "XML", "JSP");

$lang = array("PHP", "Perl", "Python", "XML", "JSP");

$alpha = wddx_serialize_value($lang);

$alpha = wddx_serialize_vars("lang");

$beta = wddx_deserialize($alpha)

$beta = wddx_deserialize($alpha);

// returns "PHP"

// returns "PHP"

print $beta[0];

print $beta["lang"][0];

// returns "Python"

// returns "Python"

print $beta[2];

print $beta["lang"][2];

?>

?>



PHP and WDDX - Page 4
XML and PHP
A Few Examples - Page 6


Up to => Home / Authoring / Languages / XML / WebServices / WDDX / XMLandPHP




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers