--- amfphp.inc.orig	2011-02-27 19:31:02.000000000 -0700
+++ amfphp.inc	2011-02-28 13:21:57.000000000 -0700
@@ -36,7 +36,7 @@
   
   // convert all userid to uid
   $args = amfphp_fix_uid($args, 0);
-  
+
   $result = services_method_call($method_name, $args);
   
   // convert all uid to userid
@@ -55,8 +55,7 @@
   $from = ($direction) ? $uid : $userid;
   $to   = (!$direction) ? $uid : $userid;
   
-  $data = _amfphp_serialize_lite($data);
-  $data = str_replace($from, $to, $data);
+  $data = _amfphp_serialize_lite_replace($data, $from, $to);
   $data = unserialize($data);
   
   return $data;
@@ -75,14 +74,27 @@
 }
 
 /*
- * A less memory intesive serializer
+ * A less memory intensive serializer that also does string replacement on
+ * object and array keys.
  * see: http://bugs.php.net/bug.php?id=39736
  */
-function _amfphp_serialize_lite(&$data, $buf = '') {
-  if (is_array($data)) {
+function _amfphp_serialize_lite_replace(&$data, $from, $to) {
+  $buf = '';
+  if (is_object($data)) {
+    $cn = get_class($data);
+    $buf.= "O:" . strlen($cn) . ':"' . $cn . '":' . count((array)$data) . ":{";
+    foreach($data as $key => &$value) {
+      $buf .= str_replace($from, $to, serialize($key)) .
+              _amfphp_serialize_lite_replace($value, $from, $to);
+    }
+    $buf.= "}";
+    return $buf;
+  }
+  else if (is_array($data)) {
     $buf.= "a:" . count($data) . ":{";
     foreach($data as $key => &$value) {
-      $buf .= serialize($key) . _amfphp_serialize_lite($value);
+      $buf .= str_replace($from, $to, serialize($key)) .
+              _amfphp_serialize_lite_replace($value, $from, $to);
     }
     $buf.= "}";
     return $buf;
@@ -91,3 +103,4 @@
     return $buf . serialize($data);
   }
 }
+
