Thrift PHP update for new Protocol wraps Transport style


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664854 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc
index 5a6b8ac..5629b73 100644
--- a/compiler/cpp/src/generate/t_php_generator.cc
+++ b/compiler/cpp/src/generate/t_php_generator.cc
@@ -175,13 +175,8 @@
   const vector<t_field*>& fields = tstruct->get_members();
   vector<t_field*>::const_iterator f_iter;
 
-  if (binary_inline_) {
-    indent(out) <<
-      "public function read($itrans) " << endl;
-  } else {
-    indent(out) <<
-      "public function read($iprot, $itrans) " << endl;
-  }
+  indent(out) <<
+    "public function read($input) " << endl;
   scope_up(out);
 
   out <<
@@ -193,7 +188,7 @@
   // Declare stack tmp variables
   if (!binary_inline_) {
     indent(out) <<
-      "$xfer += $iprot->readStructBegin($itrans, $fname);" << endl;   
+      "$xfer += $input->readStructBegin($fname);" << endl;   
   }
 
   // Loop over reading in fields
@@ -214,7 +209,7 @@
       generate_deserialize_field(out, &ffid);
     } else {
       indent(out) <<
-        "$xfer += $iprot->readFieldBegin($itrans, $fname, $ftype, $fid);" << endl;
+        "$xfer += $input->readFieldBegin($fname, $ftype, $fid);" << endl;
       // Check for field STOP marker and break
       indent(out) <<
         "if ($ftype == TType::STOP) {" << endl;
@@ -246,9 +241,9 @@
       // In the default case we skip the field
       indent(out) <<  "default:" << endl;
       if (binary_inline_) {
-        indent(out) <<  "  $xfer += TProtocol::skipBinary($itrans, $ftype);" << endl;
+        indent(out) <<  "  $xfer += TProtocol::skipBinary($input, $ftype);" << endl;
       } else {
-        indent(out) <<  "  $xfer += $iprot->skip($itrans, $ftype);" << endl;
+        indent(out) <<  "  $xfer += $input->skip($ftype);" << endl;
       }
       indent(out) <<  "  break;" << endl;
       
@@ -257,14 +252,14 @@
     if (!binary_inline_) {
       // Read field end marker
       indent(out) <<
-        "$xfer += $iprot->readFieldEnd($itrans);" << endl;
+        "$xfer += $input->readFieldEnd();" << endl;
     }
     
     scope_down(out);
     
   if (!binary_inline_) {
     indent(out) <<
-      "$xfer += $iprot->readStructEnd($itrans);" << endl;
+      "$xfer += $input->readStructEnd();" << endl;
   }
 
   indent(out) <<
@@ -287,10 +282,10 @@
 
   if (binary_inline_) {
     indent(out) <<
-      "public function write(&$_output) {" << endl;
+      "public function write(&$output) {" << endl;
   } else {
     indent(out) <<
-      "public function write($oprot, $otrans) {" << endl;
+      "public function write($output) {" << endl;
   }
   indent_up();
   
@@ -299,7 +294,7 @@
 
   if (!binary_inline_) {
     indent(out) <<
-      "$xfer += $oprot->writeStructBegin($otrans, '" << name << "');" << endl;
+      "$xfer += $output->writeStructBegin('" << name << "');" << endl;
   }
 
   for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {   
@@ -310,11 +305,11 @@
     // Write field header
     if (binary_inline_) {
       out <<
-        indent() << "$_output .= pack('c', " << type_to_enum((*f_iter)->get_type()) << ");" << endl <<
-        indent() << "$_output .= pack('n', " << (*f_iter)->get_key() << ");" << endl;
+        indent() << "$output .= pack('c', " << type_to_enum((*f_iter)->get_type()) << ");" << endl <<
+        indent() << "$output .= pack('n', " << (*f_iter)->get_key() << ");" << endl;
     } else {
       indent(out) <<
-        "$xfer += $oprot->writeFieldBegin($otrans, " <<
+        "$xfer += $output->writeFieldBegin(" <<
         "'" << (*f_iter)->get_name() << "', " <<
         type_to_enum((*f_iter)->get_type()) << ", " <<
         (*f_iter)->get_key() << ");" << endl;
@@ -326,7 +321,7 @@
     // Write field closer
     if (!binary_inline_) {
       indent(out) <<
-        "$xfer += $oprot->writeFieldEnd($otrans);" << endl;
+        "$xfer += $output->writeFieldEnd();" << endl;
     }
 
     indent_down();
@@ -336,11 +331,11 @@
 
   if (binary_inline_) {
     out <<
-      indent() << "$_output .= pack('c', TType::STOP);" << endl;
+      indent() << "$output .= pack('c', TType::STOP);" << endl;
   } else {
     out <<
-      indent() << "$xfer += $oprot->writeFieldStop($otrans);" << endl <<
-      indent() << "$xfer += $oprot->writeStructEnd($otrans);" << endl;
+      indent() << "$xfer += $output->writeFieldStop();" << endl <<
+      indent() << "$xfer += $output->writeStructEnd();" << endl;
   }
 
   out <<
@@ -412,48 +407,25 @@
 
   if (extends.empty()) {
     f_service_ <<
-      indent() << "protected $_handler = null;" << endl;
-    if (!binary_inline_) {
-      f_service_ << 
-        indent() << "protected $_iprot = null;" << endl <<
-        indent() << "protected $_oprot = null;" << endl <<
-        endl;
-    }
+      indent() << "protected $handler_ = null;" << endl;
   }
 
-  if (binary_inline_) {
+  f_service_ <<
+    indent() << "public function __construct($handler) {" << endl;
+  if (extends.empty()) {
     f_service_ <<
-      indent() << "public function __construct($handler) {" << endl;
-    if (extends.empty()) {
-      f_service_ <<
-        indent() << "  $this->_handler = $handler;" << endl;
-    } else {
-      f_service_ <<
-        indent() << "  parent::__construct($handler);" << endl;
-    }
-    f_service_ <<
-      indent() << "}" << endl <<
-      endl;
+      indent() << "  $this->handler_ = $handler;" << endl;
   } else {
     f_service_ <<
-      indent() << "public function __construct($handler, $iprot, $oprot=null) {" << endl;
-    if (extends.empty()) {
-      f_service_ <<
-        indent() << "  $this->_handler = $handler;" << endl <<
-        indent() << "  $this->_iprot = $iprot;" << endl <<
-        indent() << "  $this->_oprot = $oprot ? $oprot : $iprot;" << endl;
-    } else {
-      f_service_ <<
-        indent() << "  parent::__construct($handler, $iprot, $oprot);" << endl;
-    }
-    f_service_ <<
-      indent() << "}" << endl <<
-      endl;
+      indent() << "  parent::__construct($handler);" << endl;
   }
+  f_service_ <<
+    indent() << "}" << endl <<
+    endl;
 
   // Generate the server implementation
   indent(f_service_) <<
-    "public function process($itrans, $otrans) {" << endl;
+    "public function process($input, $output) {" << endl;
   indent_up();
 
   f_service_ <<
@@ -471,7 +443,7 @@
     generate_deserialize_field(f_service_, &fseqid, "", true);
   } else {
     f_service_ <<
-      indent() << "$this->_iprot->readMessageBegin($itrans, $fname, $mtype, $rseqid);" << endl;
+      indent() << "$input->readMessageBegin($fname, $mtype, $rseqid);" << endl;
   }
 
   // TODO(mcslee): validate message 
@@ -482,7 +454,7 @@
     indent() << "if (!method_exists($this, $methodname)) {" << endl <<
     indent() << "  throw new Exception('Function '.$fname.' not implemented.');" << endl <<
     indent() << "}" << endl <<
-    indent() << "$this->$methodname($rseqid, $itrans, $otrans);" << endl;
+    indent() << "$this->$methodname($rseqid, $input, $output);" << endl;
   indent_down();
   f_service_ <<
     indent() << "}" << endl <<
@@ -507,21 +479,18 @@
   // Open function
   indent(f_service_) <<
     "private function process_" << tfunction->get_name() <<
-    "($seqid, $itrans, $otrans) {" << endl;
+    "($seqid, $input, $output) {" << endl;
   indent_up();
 
   string argsname = service_name_ + "_" + tfunction->get_name() + "_args";
   string resultname = service_name_ + "_" + tfunction->get_name() + "_result";
 
   f_service_ <<
-    indent() << "$__args = new " << argsname << "();" << endl;
-  if (binary_inline_) {
+    indent() << "$args = new " << argsname << "();" << endl <<
+    indent() << "$args->read($input);" << endl;
+  if (!binary_inline_) {
     f_service_ <<
-      indent() << "$__args->read($itrans);" << endl;
-  } else {
-    f_service_ <<
-      indent() << "$__args->read($this->_iprot, $itrans);" << endl <<
-      indent() << "$this->_iprot->readMessageEnd($itrans);" << endl;
+      indent() << "$input->readMessageEnd();" << endl;
   }
 
   t_struct* xs = tfunction->get_xceptions();
@@ -531,7 +500,7 @@
   // Declare result for non async function
   if (!tfunction->is_async()) {
     f_service_ <<
-      indent() << "$__result = new " << resultname << "();" << endl;
+      indent() << "$result = new " << resultname << "();" << endl;
   }
 
   // Try block for a function with exceptions
@@ -548,10 +517,10 @@
 
   f_service_ << indent();
   if (!tfunction->is_async() && !tfunction->get_returntype()->is_void()) {
-    f_service_ << "$__result->success = ";
+    f_service_ << "$result->success = ";
   }
   f_service_ <<
-    "$this->_handler->" << tfunction->get_name() << "(";
+    "$this->handler_->" << tfunction->get_name() << "(";
   bool first = true;
   for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
     if (first) {
@@ -559,7 +528,7 @@
     } else {
       f_service_ << ", ";
     }
-    f_service_ << "$__args->" << (*f_iter)->get_name();
+    f_service_ << "$args->" << (*f_iter)->get_name();
   }
   f_service_ << ");" << endl;
 
@@ -571,7 +540,7 @@
       if (!tfunction->is_async()) {
         indent_up();
         f_service_ <<
-          indent() << "$__result->" << (*x_iter)->get_name() << " = $" << (*x_iter)->get_name() << ";" << endl;
+          indent() << "$result->" << (*x_iter)->get_name() << " = $" << (*x_iter)->get_name() << ";" << endl;
         indent_down();
         f_service_ << indent();
       }
@@ -584,22 +553,26 @@
     f_service_ <<
       indent() << "return;" << endl;
     indent_down();
-    f_service_ << endl;
+    f_service_ <<
+      indent() << "}" << endl;
     return;
   }
 
   // Serialize the request header
   if (binary_inline_) {
     f_service_ <<
-      indent() << "$_output = '';" << endl <<
-      indent() << "$_output .= pack('N', strlen('" << tfunction->get_name() << "'));" << endl <<
-      indent() << "$_output .= '" << tfunction->get_name() << "';" << endl <<
-      indent() << "$_output .= pack('cN', TMessageType::REPLY, $seqid);" << endl;
+      indent() << "$buff = '';" << endl <<
+      indent() << "$buff .= pack('N', strlen('" << tfunction->get_name() << "'));" << endl <<
+      indent() << "$buff .= '" << tfunction->get_name() << "';" << endl <<
+      indent() << "$buff .= pack('cN', TMessageType::REPLY, $seqid);" << endl <<
+      indent() << "$result->write($buff);" << endl <<
+      indent() << "$output->write($buff);" << endl <<
+      indent() << "$output->flush();" << endl;
   } else {
     f_service_ <<
-      indent() << "$this->_oprot->writeMessageBegin($otrans, '" << tfunction->get_name() << "', TMessageType::REPLY, $seqid);" << endl <<
-      indent() << "$__result->write($this->_oprot, $otrans);" << endl <<
-      indent() << "$otrans->flush();" << endl;
+      indent() << "$output->writeMessageBegin('" << tfunction->get_name() << "', TMessageType::REPLY, $seqid);" << endl <<
+      indent() << "$result->write($output);" << endl <<
+      indent() << "$output->getOutputTransport()->flush();" << endl;
   }
 
   // Close function
@@ -698,15 +671,9 @@
   // Private members
   if (extends.empty()) {
     f_service_ <<
-      indent() << "protected $_itrans = null;" << endl <<
-      indent() << "protected $_otrans = null;" << endl <<
+      indent() << "protected $input_ = null;" << endl <<
+      indent() << "protected $output_ = null;" << endl <<
       endl;
-    if (!binary_inline_) {
-      f_service_ <<
-        indent() << "protected $_iprot = null;" << endl <<
-        indent() << "protected $_oprot = null;" << endl <<
-        endl;
-    }
     f_service_ <<
       indent() << "protected $_seqid = 0;" << endl <<
       endl;
@@ -714,29 +681,14 @@
 
   // Constructor function
   f_service_ <<
-    indent() << "public function __construct() {" << endl;
-  f_service_ <<
-    indent() << "  $argv = func_get_args();" << endl;
+    indent() << "public function __construct($input, $output=null) {" << endl;
   if (!extends.empty()) {
     f_service_ <<
-      indent() << "  parent::__construct($argv[0], $argv[1], $argv[2], $argv[3]);" << endl;
+      indent() << "  parent::__construct($input, $output);" << endl;
   } else {
-    if (binary_inline_) {
-      f_service_ <<
-        indent() << "  $this->_itrans = $this->_otrans = $argv[0];" << endl <<
-        indent() << "  if ($argv[1]) {" << endl <<
-        indent() << "    $this->_otrans = $argv[1];" << endl <<
-        indent() << "  }" << endl;
-    } else {
-      f_service_ <<
-        indent() << "  $this->_itrans = $this->_otrans = $argv[0];" << endl <<
-        indent() << "  $this->_iprot = $this->_oprot = $argv[1];" << endl <<
-        indent() << "  if ($argv[2]) {" << endl <<
-        indent() << "    $this->_otrans = $argv[1];" << endl <<
-        indent() << "    $this->_iprot = $argv[2];" << endl <<
-        indent() << "    $this->_oprot = $argv[3];" << endl <<
-        indent() << "  }" << endl;
-    }
+    f_service_ <<
+      indent() << "  $this->input_ = $input;" << endl <<
+      indent() << "  $this->output_ = $output ? $output : $input;" << endl;
   }
   f_service_ <<
     indent() << "}" << endl << endl;   
@@ -788,38 +740,35 @@
       // Serialize the request header
       if (binary_inline_) {
         f_service_ <<
-          indent() << "$_output = '';" << endl <<
-          indent() << "$_output .= pack('N', strlen('" << funname << "'));" << endl <<
-          indent() << "$_output .= '" << funname << "';" << endl <<
-          indent() << "$_output .= pack('cN', TMessageType::CALL, $this->seqid);" << endl;
+          indent() << "$buff = '';" << endl <<
+          indent() << "$buff .= pack('N', strlen('" << funname << "'));" << endl <<
+          indent() << "$buff .= '" << funname << "';" << endl <<
+          indent() << "$buff .= pack('cN', TMessageType::CALL, $this->seqid);" << endl;
       } else {
         f_service_ <<
-          indent() << "$this->_oprot->writeMessageBegin($this->_otrans, '" << (*f_iter)->get_name() << "', TMessageType::CALL, $this->seqid);" << endl;
+          indent() << "$this->output_->writeMessageBegin('" << (*f_iter)->get_name() << "', TMessageType::CALL, $this->seqid);" << endl;
       }
       
       f_service_ <<
-        indent() << "$__args = new " << argsname << "();" << endl;
+        indent() << "$args = new " << argsname << "();" << endl;
       
       for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
         f_service_ <<
-          indent() << "$__args->" << (*fld_iter)->get_name() << " = $" << (*fld_iter)->get_name() << ";" << endl;
+          indent() << "$args->" << (*fld_iter)->get_name() << " = $" << (*fld_iter)->get_name() << ";" << endl;
       }
            
       // Write to the stream
       if (binary_inline_) { 
         f_service_ <<
-          indent() << "$__args->write($_output, $__args);" << endl <<
-          indent() << "$this->_otrans->write($_output);" << endl;
+          indent() << "$args->write($buff);" << endl <<
+          indent() << "$this->output_->write($buff);" << endl <<
+          indent() << "$this->output_->flush();" << endl;
       } else {
         f_service_ <<
-          indent() << "$__args->write($this->_oprot, $this->_otrans);" << endl <<
-          indent() << "$this->_oprot->writeMessageEnd($this->_otrans);" << endl;
-      }
-      
-      // Flush the request
-      indent(f_service_) <<
-        "$this->_otrans->flush();" << endl;
-      
+          indent() << "$args->write($this->output_);" << endl <<
+          indent() << "$this->output_->writeMessageEnd();" << endl <<
+          indent() << "$this->output_->getOutputTransport()->flush();" << endl;
+      }   
       
     scope_down(f_service_);
       
@@ -852,32 +801,26 @@
         generate_deserialize_field(f_service_, &fseqid, "", true);
       } else {
         f_service_ <<
-          indent() << "$this->_iprot->readMessageBegin($this->_itrans, $fname, $mtype, $rseqid);" << endl;
+          indent() << "$this->input_->readMessageBegin($fname, $mtype, $rseqid);" << endl;
       }
 
       // TODO(mcslee): Validate message reply here
 
       f_service_ <<
-        indent() << "$__result = new " << resultname << "();" << endl;
-      if (binary_inline_) {
-        f_service_ <<
-          indent() << "$__result->read($this->_otrans);" << endl;
-      } else {
-        f_service_ <<
-          indent() << "$__result->read($this->_iprot, $this->_otrans);" << endl;
-      }
+        indent() << "$result = new " << resultname << "();" << endl <<
+        indent() << "$result->read($this->input_);" << endl;
 
       if (!binary_inline_) {
         f_service_ <<
-          indent() << "$this->_iprot->readMessageEnd($this->_itrans);" << endl <<
+          indent() << "$this->input_->readMessageEnd();" << endl <<
           endl;
       }
 
-      // Careful, only return _result if not a void function
+      // Careful, only return result if not a void function
       if (!(*f_iter)->get_returntype()->is_void()) {
         f_service_ <<
-          indent() << "if ($__result->success !== null) {" << endl <<
-          indent() << "  return $__result->success;" << endl <<
+          indent() << "if ($result->success !== null) {" << endl <<
+          indent() << "  return $result->success;" << endl <<
           indent() << "}" << endl;
       }
 
@@ -886,8 +829,8 @@
       vector<t_field*>::const_iterator x_iter;
       for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
         f_service_ <<
-          indent() << "if ($__result->" << (*x_iter)->get_name() << " !== null) {" << endl <<
-          indent() << "  throw $__result->" << (*x_iter)->get_name() << ";" << endl <<
+          indent() << "if ($result->" << (*x_iter)->get_name() << " !== null) {" << endl <<
+          indent() << "  throw $result->" << (*x_iter)->get_name() << ";" << endl <<
           indent() << "}" << endl;
       }
 
@@ -940,7 +883,7 @@
   } else if (type->is_base_type() || type->is_enum()) {
 
     if (binary_inline_) {
-      std::string itrans = inclass ? "$this->_itrans" : "$itrans";
+      std::string itrans = "$input";
 
       if (type->is_base_type()) {
         t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
@@ -951,12 +894,12 @@
           break;
         case t_base_type::TYPE_STRING:
           out <<
-            indent() << "$_len = unpack('N', " << itrans << "->readAll(4));" << endl <<
-            indent() << "$_len = $_len[1];" << endl <<
-            indent() << "if ($_len > 0x7fffffff) {" << endl <<
-            indent() << "  $_len = 0 - (($len - 1) ^ 0xffffffff);" << endl <<
+            indent() << "$len = unpack('N', " << itrans << "->readAll(4));" << endl <<
+            indent() << "$len = $len[1];" << endl <<
+            indent() << "if ($len > 0x7fffffff) {" << endl <<
+            indent() << "  $len = 0 - (($len - 1) ^ 0xffffffff);" << endl <<
             indent() << "}" << endl <<
-            indent() << "$" << name << " = " << itrans << "->readAll($_len);" << endl;
+            indent() << "$" << name << " = " << itrans << "->readAll($len);" << endl;
           break;
         case t_base_type::TYPE_BOOL:
           out <<
@@ -970,54 +913,54 @@
           break;
         case t_base_type::TYPE_I16:
           out <<
-            indent() << "$_val = unpack('n', " << itrans << "->readAll(2));" << endl <<
-            indent() << "$_val = $_val[1];" << endl <<
-            indent() << "if ($_val > 0x7fff) {" << endl <<
-            indent() << "  $_val = 0 - (($_val - 1) ^ 0xffff);" << endl <<
+            indent() << "$val = unpack('n', " << itrans << "->readAll(2));" << endl <<
+            indent() << "$val = $val[1];" << endl <<
+            indent() << "if ($val > 0x7fff) {" << endl <<
+            indent() << "  $val = 0 - (($val - 1) ^ 0xffff);" << endl <<
             indent() << "}" << endl <<
-            indent() << "$" << name << " = $_val;" << endl;
+            indent() << "$" << name << " = $val;" << endl;
           break;
         case t_base_type::TYPE_I32:
           out <<
-            indent() << "$_val = unpack('N', " << itrans << "->readAll(4));" << endl <<
-            indent() << "$_val = $_val[1];" << endl <<
-            indent() << "if ($_val > 0x7fffffff) {" << endl <<
-            indent() << "  $_val = 0 - (($_val - 1) ^ 0xffffffff);" << endl <<
+            indent() << "$val = unpack('N', " << itrans << "->readAll(4));" << endl <<
+            indent() << "$val = $val[1];" << endl <<
+            indent() << "if ($val > 0x7fffffff) {" << endl <<
+            indent() << "  $val = 0 - (($val - 1) ^ 0xffffffff);" << endl <<
             indent() << "}" << endl <<
-            indent() << "$" << name << " = $_val;" << endl;
+            indent() << "$" << name << " = $val;" << endl;
           break;
         case t_base_type::TYPE_I64:
           out <<
-            indent() << "$_arr = unpack('N2', " << itrans << "->readAll(8));" << endl <<
-            indent() << "if ($_arr[1] & 0x80000000) {" << endl <<
-            indent() << "  $_arr[1] = $_arr[1] ^ 0xFFFFFFFF;" << endl <<
-            indent() << "  $_arr[2] = $_arr[2] ^ 0xFFFFFFFF;" << endl <<
-            indent() << "  $" << name << " = 0 - $_arr[1]*4294967296 - $_arr[2] - 1;" << endl <<
+            indent() << "$arr = unpack('N2', " << itrans << "->readAll(8));" << endl <<
+            indent() << "if ($arr[1] & 0x80000000) {" << endl <<
+            indent() << "  $arr[1] = $arr[1] ^ 0xFFFFFFFF;" << endl <<
+            indent() << "  $arr[2] = $arr[2] ^ 0xFFFFFFFF;" << endl <<
+            indent() << "  $" << name << " = 0 - $arr[1]*4294967296 - $arr[2] - 1;" << endl <<
             indent() << "} else {" << endl <<
-            indent() << "  $" << name << " = $_arr[1]*4294967296 + $_arr[2];" << endl <<
+            indent() << "  $" << name << " = $arr[1]*4294967296 + $arr[2];" << endl <<
             indent() << "}" << endl;
           break;
         case t_base_type::TYPE_DOUBLE:
           out <<
-            indent() << "$_arr = unpack('d', strrev(" << itrans << "->readAll(8)));" << endl <<
-            indent() << "$" << name << " = $_arr[1];" << endl;
+            indent() << "$arr = unpack('d', strrev(" << itrans << "->readAll(8)));" << endl <<
+            indent() << "$" << name << " = $arr[1];" << endl;
           break;
         default:
           throw "compiler error: no PHP name for base type " + tbase + tfield->get_name();
         }
       } else if (type->is_enum()) {
           out <<
-            indent() << "$_val = unpack('N', " << itrans << "->readAll(4));" << endl <<
-            indent() << "$_val = $_val[1];" << endl <<
-            indent() << "if ($_val > 0x7fffffff) {" << endl <<
-            indent() << "  $_val = 0 - (($_val - 1) ^ 0xffffffff);" << endl <<
+            indent() << "$val = unpack('N', " << itrans << "->readAll(4));" << endl <<
+            indent() << "$val = $val[1];" << endl <<
+            indent() << "if ($val > 0x7fffffff) {" << endl <<
+            indent() << "  $val = 0 - (($val - 1) ^ 0xffffffff);" << endl <<
             indent() << "}" << endl <<
-            indent() << "$" << name << " = $_val;" << endl;
+            indent() << "$" << name << " = $val;" << endl;
       }
     } else {
 
       indent(out) <<
-        "$xfer += $iprot->";
+        "$xfer += $input->";
       
       if (type->is_base_type()) {
         t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
@@ -1027,31 +970,31 @@
             name;
           break;
         case t_base_type::TYPE_STRING:        
-          out << "readString($itrans, $" << name << ");";
+          out << "readString($" << name << ");";
           break;
         case t_base_type::TYPE_BOOL:
-          out << "readBool($itrans, $" << name << ");";
+          out << "readBool($" << name << ");";
           break;
         case t_base_type::TYPE_BYTE:
-          out << "readByte($itrans, $" << name << ");";
+          out << "readByte($" << name << ");";
           break;
         case t_base_type::TYPE_I16:
-          out << "readI16($itrans, $" << name << ");";
+          out << "readI16($" << name << ");";
           break;
         case t_base_type::TYPE_I32:
-          out << "readI32($itrans, $" << name << ");";
+          out << "readI32($" << name << ");";
           break;
         case t_base_type::TYPE_I64:
-          out << "readI64($itrans, $" << name << ");";
+          out << "readI64($" << name << ");";
           break;
         case t_base_type::TYPE_DOUBLE:
-          out << "readDouble($itrans, $" << name << ");";
+          out << "readDouble($" << name << ");";
           break;
         default:
           throw "compiler error: no PHP name for base type " + tbase;
         }
       } else if (type->is_enum()) {
-        out << "readI32($itrans, $" << name << ");";
+        out << "readI32($" << name << ");";
       }
       out << endl;
     }
@@ -1072,14 +1015,8 @@
                                                   t_struct* tstruct,
                                                   string prefix) {
   out <<
-    indent() << "$" << prefix << " = new " << tstruct->get_name() << "();" << endl;
-  if (binary_inline_) {
-    out <<
-      indent() << "$xfer += $" << prefix << "->read($itrans);" << endl;
-  } else {
-    out <<
-      indent() << "$xfer += $" << prefix << "->read($iprot, $itrans);" << endl;
-  }
+    indent() << "$" << prefix << " = new " << tstruct->get_name() << "();" << endl <<
+    indent() << "$xfer += $" << prefix << "->read($input);" << endl;
 }
 
 void t_php_generator::generate_deserialize_container(ofstream &out,
@@ -1112,7 +1049,7 @@
       generate_deserialize_field(out, &fsize);
     } else {
       out <<
-        indent() << "$xfer += $iprot->readMapBegin($itrans, " <<
+        indent() << "$xfer += $input->readMapBegin(" <<
         "$" << ktype << ", $" << vtype << ", $" << size << ");" << endl;
     }
   } else if (ttype->is_set()) {
@@ -1122,7 +1059,7 @@
     } else {
       out <<
         indent() << "$" << etype << " = 0;" << endl <<
-        indent() << "$xfer += $iprot->readSetBegin($itrans, " <<
+        indent() << "$xfer += $input->readSetBegin(" <<
         "$" << etype << ", $" << size << ");" << endl;
     }
   } else if (ttype->is_list()) {
@@ -1132,7 +1069,7 @@
     } else {
       out <<
         indent() << "$" << etype << " = 0;" << endl <<
-        indent() << "$xfer += $iprot->readListBegin($itrans, " <<
+        indent() << "$xfer += $input->readListBegin(" <<
         "$" << etype << ", $" << size << ");" << endl;
     }
   }
@@ -1158,11 +1095,11 @@
   if (!binary_inline_) {
     // Read container end
     if (ttype->is_map()) {
-      indent(out) << "$xfer += $iprot->readMapEnd($itrans);" << endl;
+      indent(out) << "$xfer += $input->readMapEnd();" << endl;
     } else if (ttype->is_set()) {
-      indent(out) << "$xfer += $iprot->readSetEnd($itrans);" << endl;
+      indent(out) << "$xfer += $input->readSetEnd();" << endl;
     } else if (ttype->is_list()) {
-      indent(out) << "$xfer += $iprot->readListEnd($itrans);" << endl;
+      indent(out) << "$xfer += $input->readListEnd();" << endl;
     }
   }
 
@@ -1176,8 +1113,8 @@
 void t_php_generator::generate_deserialize_map_element(ofstream &out,
                                                        t_map* tmap,
                                                        string prefix) {
-  string key = tmp("_key");
-  string val = tmp("_val");
+  string key = tmp("key");
+  string val = tmp("val");
   t_field fkey(tmap->get_key_type(), key);
   t_field fval(tmap->get_val_type(), val);
 
@@ -1196,7 +1133,7 @@
 void t_php_generator::generate_deserialize_set_element(ofstream &out,
                                                        t_set* tset,
                                                        string prefix) {
-  string elem = tmp("_elem");
+  string elem = tmp("elem");
   t_field felem(tset->get_elem_type(), elem);
 
   indent(out) <<
@@ -1211,7 +1148,7 @@
 void t_php_generator::generate_deserialize_list_element(ofstream &out,
                                                         t_list* tlist,
                                                         string prefix) {
-  string elem = tmp("_elem");
+  string elem = tmp("elem");
   t_field felem(tlist->get_elem_type(), elem);
 
   indent(out) <<
@@ -1266,44 +1203,44 @@
           break;
         case t_base_type::TYPE_STRING:
           out <<
-            indent() << "$_output .= pack('N', strlen($" << name << "));" << endl <<
-            indent() << "$_output .= $" << name << ";" << endl;
+            indent() << "$output .= pack('N', strlen($" << name << "));" << endl <<
+            indent() << "$output .= $" << name << ";" << endl;
           break;
         case t_base_type::TYPE_BOOL:
           out <<
-            indent() << "$_output .= pack('c', $" << name << " ? 1 : 0);" << endl;
+            indent() << "$output .= pack('c', $" << name << " ? 1 : 0);" << endl;
           break;
         case t_base_type::TYPE_BYTE:
           out <<
-            indent() << "$_output .= pack('c', $" << name << ");" << endl;
+            indent() << "$output .= pack('c', $" << name << ");" << endl;
           break;
         case t_base_type::TYPE_I16:
           out <<
-            indent() << "$_output .= pack('n', $" << name << ");" << endl;
+            indent() << "$output .= pack('n', $" << name << ");" << endl;
           break;
         case t_base_type::TYPE_I32:
           out <<
-            indent() << "$_output .= pack('N', $" << name << ");" << endl;
+            indent() << "$output .= pack('N', $" << name << ");" << endl;
           break;
         case t_base_type::TYPE_I64:
           out << 
-            indent() << "$_output .= pack('N2', $" << name << " >> 32, $" << name << " & 0xFFFFFFFF);" << endl;
+            indent() << "$output .= pack('N2', $" << name << " >> 32, $" << name << " & 0xFFFFFFFF);" << endl;
           break;
         case t_base_type::TYPE_DOUBLE:
           out << 
-            indent() << "$_output .= strrev(pack('d', $" << name << "));" << endl;
+            indent() << "$output .= strrev(pack('d', $" << name << "));" << endl;
           break;
         default:
           throw "compiler error: no PHP name for base type " + tbase;
         }
       } else if (type->is_enum()) {
         out <<
-          indent() << "$_output .= pack('N', $" << name << ");" << endl;
+          indent() << "$output .= pack('N', $" << name << ");" << endl;
       }
     } else {
 
       indent(out) <<
-        "$xfer += $oprot->";
+        "$xfer += $output->";
       
       if (type->is_base_type()) {
         t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
@@ -1313,31 +1250,31 @@
             "compiler error: cannot serialize void field in a struct: " + name;
           break;
         case t_base_type::TYPE_STRING:
-          out << "writeString($otrans, $" << name << ");";
+          out << "writeString($" << name << ");";
           break;
         case t_base_type::TYPE_BOOL:
-          out << "writeBool($otrans, $" << name << ");";
+          out << "writeBool($" << name << ");";
           break;
         case t_base_type::TYPE_BYTE:
-          out << "writeByte($otrans, $" << name << ");";
+          out << "writeByte($" << name << ");";
           break;
         case t_base_type::TYPE_I16:
-          out << "writeI16($otrans, $" << name << ");";
+          out << "writeI16($" << name << ");";
           break;
         case t_base_type::TYPE_I32:
-          out << "writeI32($otrans, $" << name << ");";
+          out << "writeI32($" << name << ");";
           break;
         case t_base_type::TYPE_I64:
-          out << "writeI64($otrans, $" << name << ");";
+          out << "writeI64($" << name << ");";
           break;
         case t_base_type::TYPE_DOUBLE:
-          out << "writeDouble($otrans, $" << name << ");";
+          out << "writeDouble($" << name << ");";
           break;
         default:
           throw "compiler error: no PHP name for base type " + tbase;
         }
       } else if (type->is_enum()) {
-        out << "writeI32($otrans, $" << name << ");";
+        out << "writeI32($" << name << ");";
       }
       out << endl;
     }
@@ -1358,13 +1295,8 @@
 void t_php_generator::generate_serialize_struct(ofstream &out,
                                                 t_struct* tstruct,
                                                 string prefix) {
-  if (binary_inline_) {
-    indent(out) <<
-      "$xfer += $" << prefix << "->write($_output);" << endl;
-  } else {
-    indent(out) <<
-      "$xfer += $" << prefix << "->write($oprot, $otrans);" << endl;
-  }
+  indent(out) <<
+    "$xfer += $" << prefix << "->write($output);" << endl; 
 }
 
 /**
@@ -1378,12 +1310,12 @@
   if (ttype->is_map()) {
     if (binary_inline_) {
       out <<
-        indent() << "$_output .= pack('c', " << type_to_enum(((t_map*)ttype)->get_key_type()) << ");" << endl <<
-        indent() << "$_output .= pack('c', " << type_to_enum(((t_map*)ttype)->get_val_type()) << ");" << endl <<
-        indent() << "$_output .= strrev(pack('l', count($" << prefix << ")));" << endl;
+        indent() << "$output .= pack('c', " << type_to_enum(((t_map*)ttype)->get_key_type()) << ");" << endl <<
+        indent() << "$output .= pack('c', " << type_to_enum(((t_map*)ttype)->get_val_type()) << ");" << endl <<
+        indent() << "$output .= strrev(pack('l', count($" << prefix << ")));" << endl;
     } else {
       indent(out) <<
-        "$oprot->writeMapBegin($otrans, " <<
+        "$output->writeMapBegin(" <<
         type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
         type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
         "count($" << prefix << "));" << endl;
@@ -1391,24 +1323,24 @@
   } else if (ttype->is_set()) {
     if (binary_inline_) {
       out <<
-        indent() << "$_output .= pack('c', " << type_to_enum(((t_set*)ttype)->get_elem_type()) << ");" << endl <<
-        indent() << "$_output .= strrev(pack('l', count($" << prefix << ")));" << endl;
+        indent() << "$output .= pack('c', " << type_to_enum(((t_set*)ttype)->get_elem_type()) << ");" << endl <<
+        indent() << "$output .= strrev(pack('l', count($" << prefix << ")));" << endl;
 
     } else {
       indent(out) <<
-        "$oprot->writeSetBegin($otrans, " <<
+        "$output->writeSetBegin(" <<
         type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
         "count($" << prefix << "));" << endl;
     }
   } else if (ttype->is_list()) {
     if (binary_inline_) {
       out <<
-        indent() << "$_output .= pack('c', " << type_to_enum(((t_list*)ttype)->get_elem_type()) << ");" << endl <<
-        indent() << "$_output .= strrev(pack('l', count($" << prefix << ")));" << endl;
+        indent() << "$output .= pack('c', " << type_to_enum(((t_list*)ttype)->get_elem_type()) << ");" << endl <<
+        indent() << "$output .= strrev(pack('l', count($" << prefix << ")));" << endl;
 
     } else {
       indent(out) <<
-        "$oprot->writeListBegin($otrans, " <<
+        "$output->writeListBegin(" <<
         type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
         "count($" << prefix << "));" << endl;
     }
@@ -1417,8 +1349,8 @@
     scope_up(out);
 
     if (ttype->is_map()) {
-      string kiter = tmp("_kiter");
-      string viter = tmp("_viter");
+      string kiter = tmp("kiter");
+      string viter = tmp("viter");
       indent(out) << 
         "foreach ($" << prefix << " as " <<
         "$" << kiter << " => $" << viter << ")" << endl;
@@ -1426,14 +1358,14 @@
       generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
       scope_down(out);
     } else if (ttype->is_set()) {
-      string iter = tmp("_iter");
+      string iter = tmp("iter");
       indent(out) << 
         "foreach ($" << prefix << " as $" << iter << ")" << endl;
       scope_up(out);
       generate_serialize_set_element(out, (t_set*)ttype, iter);
       scope_down(out);
     } else if (ttype->is_list()) {
-      string iter = tmp("_iter");
+      string iter = tmp("iter");
       indent(out) << 
         "foreach ($" << prefix << " as $" << iter << ")" << endl;
       scope_up(out);
@@ -1446,13 +1378,13 @@
   if (!binary_inline_) {
     if (ttype->is_map()) {
       indent(out) <<
-        "$oprot->writeMapEnd($otrans);" << endl;
+        "$output->writeMapEnd();" << endl;
     } else if (ttype->is_set()) {
       indent(out) <<
-        "$oprot->writeSetEnd($otrans);" << endl;
+        "$output->writeSetEnd();" << endl;
     } else if (ttype->is_list()) {
       indent(out) <<
-        "$oprot->writeListEnd($otrans);" << endl;
+        "$output->writeListEnd();" << endl;
     }
   }
  
diff --git a/lib/php/src/protocol/TBinaryProtocol.php b/lib/php/src/protocol/TBinaryProtocol.php
index dd4408f..7684f5f 100644
--- a/lib/php/src/protocol/TBinaryProtocol.php
+++ b/lib/php/src/protocol/TBinaryProtocol.php
@@ -11,96 +11,100 @@
  */
 class TBinaryProtocol extends TProtocol {
 
-  public function writeMessageBegin($out, $name, $type, $seqid) {
+  public function __construct($in, $out=null) {
+    parent::__construct($in, $out);
+  }
+
+  public function writeMessageBegin($name, $type, $seqid) {
     return 
-      $this->writeString($out, $name) +
-      $this->writeByte($out, $type) +
-      $this->writeI32($out, $seqid);
+      $this->writeString($name) +
+      $this->writeByte($type) +
+      $this->writeI32($seqid);
   }
 
-  public function writeMessageEnd($out) {
+  public function writeMessageEnd() {
     return 0;
   }
 
-  public function writeStructBegin($out, $name) {
+  public function writeStructBegin($name) {
     return 0;
   }
 
-  public function writeStructEnd($out) {
+  public function writeStructEnd() {
     return 0;
   }
 
-  public function writeFieldBegin($out, $fieldName, $fieldType, $fieldId) {
+  public function writeFieldBegin($fieldName, $fieldType, $fieldId) {
     return
-      $this->writeByte($out, $fieldType) +
-      $this->writeI16($out, $fieldId);
+      $this->writeByte($fieldType) +
+      $this->writeI16($fieldId);
   }
 
-  public function writeFieldEnd($out) {
+  public function writeFieldEnd() {
     return 0;
   } 
 
-  public function writeFieldStop($out) {
+  public function writeFieldStop() {
     return
-      $this->writeByte($out, TType::STOP);
+      $this->writeByte(TType::STOP);
   }
 
-  public function writeMapBegin($out, $keyType, $valType, $size) {
+  public function writeMapBegin($keyType, $valType, $size) {
     return
-      $this->writeByte($out, $keyType) +
-      $this->writeByte($out, $valType) +
-      $this->writeI32($out, $size);
+      $this->writeByte($keyType) +
+      $this->writeByte($valType) +
+      $this->writeI32($size);
   }
 
-  public function writeMapEnd($out) {
+  public function writeMapEnd() {
     return 0;
   }
 
-  public function writeListBegin($out, $elemType, $size) {
+  public function writeListBegin($elemType, $size) {
     return
-      $this->writeByte($out, $elemType) +
-      $this->writeI32($out, $size);
+      $this->writeByte($elemType) +
+      $this->writeI32($size);
   }
 
-  public function writeListEnd($out) {
+  public function writeListEnd() {
     return 0;
   }
 
-  public function writeSetBegin($out, $elemType, $size) {
+  public function writeSetBegin($elemType, $size) {
     return
-      $this->writeByte($out, $elemType) +
-      $this->writeI32($out, $size);
+      $this->writeByte($elemType) +
+      $this->writeI32($size);
   }
 
-  public function writeSetEnd($out) {
+  public function writeSetEnd() {
     return 0;
   }
 
-  public function writeBool($out, $value) {
+  public function writeBool($value) {
     $data = pack('c', $value ? 1 : 0);
-    $out->write($data, 1);
+    $this->outputTransport_->write($data, 1);
     return 1;
   }
 
-  public function writeByte($out, $value) {
+  public function writeByte($value) {
     $data = pack('c', $value);
-    $out->write($data, 1);
+    $this->outputTransport_->write($data, 1);
     return 1;
   }
 
-  public function writeI16($out, $value) {
+  public function writeI16($value) {
     $data = pack('n', $value);
-    $out->write($data, 2);
+    $this->outputTransport_->write($data, 2);
     return 2;
   }
 
-  public function writeI32($out, $value) {
+  public function writeI32($value) {
     $data = pack('N', $value);
-    $out->write($data, 4);
+    $this->outputTransport_->write($data, 4);
     return 4;
   }
 
-  public function writeI64($out, $value) {
+  public function writeI64($value) {
     // If we are on a 32bit architecture we have to explicitly deal with
     // 64-bit twos-complement arithmetic since PHP wants to treat all ints
     // as signed and any int over 2^31 - 1 as a float   
@@ -132,106 +136,106 @@
       $data = pack('N2', $hi, $lo);
     }
 
-    $out->write($data, 8);
+    $this->outputTransport_->write($data, 8);
     return 8;
   }
 
-  public function writeDouble($out, $value) {
+  public function writeDouble($value) {
     $data = pack('d', $value);
-    $out->write(strrev($data), 8);
+    $this->outputTransport_->write(strrev($data), 8);
     return 8;
   }
 
-  public function writeString($out, $value) {
+  public function writeString($value) {
     $len = strlen($value);
-    $result = $this->writeI32($out, $len);
+    $result = $this->writeI32($len);
     if ($len) {
-      $out->write($value, $len);
+      $this->outputTransport_->write($value, $len);
     }
     return $result + $len;
   }
 
-  public function readMessageBegin($in, &$name, &$type, &$seqid) {
+  public function readMessageBegin(&$name, &$type, &$seqid) {
     return 
-      $this->readString($in, $name) +
-      $this->readByte($in, $type) +
-      $this->readI32($in, $seqid);
+      $this->readString($name) +
+      $this->readByte($type) +
+      $this->readI32($seqid);
   }
 
-  public function readMessageEnd($out) {
+  public function readMessageEnd() {
     return 0;
   }
 
-  public function readStructBegin($in, &$name) {
+  public function readStructBegin(&$name) {
     $name = '';
     return 0;
   }
 
-  public function readStructEnd($in) {
+  public function readStructEnd() {
     return 0;
   }
 
-  public function readFieldBegin($in, &$name, &$fieldType, &$fieldId) {
-    $result = $this->readByte($in, $fieldType);
+  public function readFieldBegin(&$name, &$fieldType, &$fieldId) {
+    $result = $this->readByte($fieldType);
     if ($fieldType == TType::STOP) {
       $fieldId = 0;
       return $result;
     }
-    $result += $this->readI16($in, $fieldId);
+    $result += $this->readI16($fieldId);
     return $result;
   }
 
-  public function readFieldEnd($in) {
+  public function readFieldEnd() {
     return 0;
   }
 
-  public function readMapBegin($in, &$keyType, &$valType, &$size) {
+  public function readMapBegin(&$keyType, &$valType, &$size) {
     return
-      $this->readByte($in, $keyType) +
-      $this->readByte($in, $valType) +
-      $this->readI32($in, $size);
+      $this->readByte($keyType) +
+      $this->readByte($valType) +
+      $this->readI32($size);
   }
 
-  public function readMapEnd($in) {
+  public function readMapEnd() {
     return 0;
   }
 
-  public function readListBegin($in, &$elemType, &$size) {
+  public function readListBegin(&$elemType, &$size) {
     return
-      $this->readByte($in, $elemType) +
-      $this->readI32($in, $size);
+      $this->readByte($elemType) +
+      $this->readI32($size);
   }
 
-  public function readListEnd($in) {
+  public function readListEnd() {
     return 0;
   }
 
-  public function readSetBegin($in, &$elemType, &$size) {
+  public function readSetBegin(&$elemType, &$size) {
     return
-      $this->readByte($in, $elemType) +
-      $this->readI32($in, $size);
+      $this->readByte($elemType) +
+      $this->readI32($size);
   }
 
-  public function readSetEnd($in) {
+  public function readSetEnd() {
     return 0;
   }
 
-  public function readBool($in, &$value) {
-    $data = $in->readAll(1);
+  public function readBool(&$value) {
+    $data = $this->inputTransport_->readAll(1);
     $arr = unpack('c', $data);
     $value = $arr[1] == 1;
     return 1;
   }
 
-  public function readByte($in, &$value) {
-    $data = $in->readAll(1);
+  public function readByte(&$value) {
+    $data = $this->inputTransport_->readAll(1);
     $arr = unpack('c', $data);
     $value = $arr[1];
     return 1;
   }
 
-  public function readI16($in, &$value) {
-    $data = $in->readAll(2);
+  public function readI16(&$value) {
+    $data = $this->inputTransport_->readAll(2);
     $arr = unpack('n', $data);
     $value = $arr[1];
     if ($value > 0x7fff) {
@@ -240,8 +244,8 @@
     return 2;
   }
 
-  public function readI32($in, &$value) {
-    $data = $in->readAll(4);
+  public function readI32(&$value) {
+    $data = $this->inputTransport_->readAll(4);
     $arr = unpack('N', $data);
     $value = $arr[1];
     if ($value > 0x7fffffff) {
@@ -250,8 +254,8 @@
     return 4;
   }
 
-  public function readI64($in, &$value) {
-    $data = $in->readAll(8);
+  public function readI64(&$value) {
+    $data = $this->inputTransport_->readAll(8);
 
     $arr = unpack('N2', $data);
     
@@ -310,17 +314,17 @@
     return 8;
   }
 
-  public function readDouble($in, &$value) {
-    $data = strrev($in->readAll(8));
+  public function readDouble(&$value) {
+    $data = strrev($this->inputTransport_->readAll(8));
     $arr = unpack('d', $data);
     $value = $arr[1];
     return 8;
   }
 
-  public function readString($in, &$value) {
-    $result = $this->readI32($in, $len);
+  public function readString(&$value) {
+    $result = $this->readI32($len);
     if ($len) {
-      $value = $in->readAll($len);
+      $value = $this->inputTransport_->readAll($len);
     } else {
       $value = '';
     }
@@ -328,4 +332,14 @@
   }
 }
 
+/**
+ * Binary Protocol Factory
+ */
+class TBinaryProtocolFactory implements TProtocolFactory {
+  public function getIOProtocols($itrans, $otrans) {
+    $prot = new TBinaryProtocol($itrans, $otrans);
+    return array($prot, $prot);
+  }
+}
+
 ?>
diff --git a/lib/php/src/protocol/TProtocol.php b/lib/php/src/protocol/TProtocol.php
index 40266dd..2ef42c1 100644
--- a/lib/php/src/protocol/TProtocol.php
+++ b/lib/php/src/protocol/TProtocol.php
@@ -1,212 +1,271 @@
 <?php
 
 /**
- * For Type Constants
- */
-include_once $GLOBALS['THRIFT_ROOT'].'/protocol/TType.php';
-
-/**
- * Protocol module.
+ * Protocol module. Contains all the types and definitions needed to implement
+ * a protocol encoder/decoder.
  *
  * @package thrift.protocol
  * @author Mark Slee <mcslee@facebook.com>
  */
+
+/**
+ * Data types that can be sent via Thrift
+ */
+class TType {
+  const STOP   = 0;
+  const VOID   = 1;
+  const BOOL   = 2;
+  const BYTE   = 3;
+  const I08    = 3;
+  const DOUBLE = 4;
+  const I16    = 6;
+  const I32    = 8;
+  const I64    = 10;
+  const STRING = 11;
+  const UTF7   = 11;
+  const STRUCT = 12;
+  const MAP    = 13;
+  const SET    = 14;
+  const LST    = 15;    // N.B. cannot use LIST keyword in PHP!
+  const UTF8   = 16;
+  const UTF16  = 17;
+}
+
+/**
+ * Message types for RPC
+ */
+class TMessageType {
+  const CALL  = 1;
+  const REPLY = 2;
+}
+
+/**
+ * Protocol base class module.
+ */
 abstract class TProtocol {
 
+  /**
+   * Input transport
+   *
+   * @var TTransport
+   */
+  protected $inputTransport_;
+
+  /**
+   * Output transport
+   *
+   * @var TTransport
+   */
+  protected $outputTransport_;
+
+  /**
+   * Constructor
+   */
+  protected function __construct($in, $out=null) {
+    $this->inputTransport_ = $in;
+    $this->outputTransport_ = $out ? $out : $in;
+  }
+
+  /**
+   * Accessor for input
+   *
+   * @return TTransport
+   */
+  public function getInputTransport() {
+    return $this->inputTransport_;
+  }
+
+  /**
+   * Accessor for output
+   *
+   * @return TTransport
+   */
+  public function getOutputTransport() {
+    return $this->outputTransport_;
+  }
+
   /** 
    * Writes the message header
    *
-   * @param TTransport $out Output transport
    * @param string $name Function name
    * @param int $type message type TMessageType::CALL or TMessageType::REPLY
    * @param int $seqid The sequence id of this message
    */
-  public abstract function writeMessageBegin($out, $name, $type, $seqid);
+  public abstract function writeMessageBegin($name, $type, $seqid);
 
   /**
    * Close the message
-   *
-   * @param TTransport $out Output transport
    */
-  public abstract function writeMessageEnd($out);
+  public abstract function writeMessageEnd();
 
   /**
    * Writes a struct header.
    *
-   * @param TTransport $out  Output transport
    * @param string     $name Struct name
    * @throws TException on write error
    * @return int How many bytes written
    */
-  public abstract function writeStructBegin($out, $name);
-
+  public abstract function writeStructBegin($name);
 
   /**
    * Close a struct.
    *
-   * @param TTransport $out Output transport
    * @throws TException on write error
    * @return int How many bytes written
    */
-  public abstract function writeStructEnd($out);
+  public abstract function writeStructEnd();
 
   /*
    * Starts a field.
    *
-   * @param TTransport $out  Output transport
    * @param string     $name Field name
    * @param int        $type Field type
    * @param int        $fid  Field id
    * @throws TException on write error
    * @return int How many bytes written
    */
-  public abstract function writeFieldBegin($out, $fieldName, $fieldType, $fieldId);
+  public abstract function writeFieldBegin($fieldName, $fieldType, $fieldId);
 
-  public abstract function writeFieldEnd($out);
+  public abstract function writeFieldEnd();
 
-  public abstract function writeFieldStop($out);
+  public abstract function writeFieldStop();
 
-  public abstract function writeMapBegin($out, $keyType, $valType, $size);
+  public abstract function writeMapBegin($keyType, $valType, $size);
 
-  public abstract function writeMapEnd($out);
+  public abstract function writeMapEnd();
   
-  public abstract function writeListBegin($out, $elemType, $size);
+  public abstract function writeListBegin($elemType, $size);
   
-  public abstract function writeListEnd($out);
+  public abstract function writeListEnd();
 
-  public abstract function writeSetBegin($out, $elemType, $size);
+  public abstract function writeSetBegin($elemType, $size);
 
-  public abstract function writeSetEnd($out);
+  public abstract function writeSetEnd();
   
-  public abstract function writeBool($out, $bool);
+  public abstract function writeBool($bool);
 
-  public abstract function writeByte($out, $byte);
+  public abstract function writeByte($byte);
   
-  public abstract function writeI16($out, $i16);
+  public abstract function writeI16($i16);
 
-  public abstract function writeI32($out, $i32);
+  public abstract function writeI32($i32);
 
-  public abstract function writeI64($out, $i64);
+  public abstract function writeI64($i64);
 
-  public abstract function writeDouble($out, $dub);
+  public abstract function writeDouble($dub);
 
-  public abstract function writeString($out, $str);
-
+  public abstract function writeString($str);
 
   /**
    * Reads the message header
    *
-   * @param TTransport $out Output transport
    * @param string $name Function name
    * @param int $type message type TMessageType::CALL or TMessageType::REPLY
    * @parem int $seqid The sequence id of this message
    */
-  public abstract function readMessageBegin($in, &$name, &$type, &$seqid);
+  public abstract function readMessageBegin(&$name, &$type, &$seqid);
 
   /**
    * Read the close of message
-   *
-   * @param TTransport $out Output transport
    */
-  public abstract function readMessageEnd($in);
+  public abstract function readMessageEnd();
 
-  public abstract function readStructBegin($in, &$name);
+  public abstract function readStructBegin(&$name);
   
-  public abstract function readStructEnd($in);
+  public abstract function readStructEnd();
 
-  public abstract function readFieldBegin($in, &$name, &$fieldType, &$fieldId);
+  public abstract function readFieldBegin(&$name, &$fieldType, &$fieldId);
 
-  public abstract function readFieldEnd($in);
+  public abstract function readFieldEnd();
 
-  public abstract function readMapBegin($in, &$keyType, &$valType, &$size);
+  public abstract function readMapBegin(&$keyType, &$valType, &$size);
 
-  public abstract function readMapEnd($in);
+  public abstract function readMapEnd();
 
-  public abstract function readListBegin($in, &$elemType, &$size);
+  public abstract function readListBegin(&$elemType, &$size);
   
-  public abstract function readListEnd($in);
+  public abstract function readListEnd();
 
-  public abstract function readSetBegin($in, &$elemType, &$size);
+  public abstract function readSetBegin(&$elemType, &$size);
   
-  public abstract function readSetEnd($in);
+  public abstract function readSetEnd();
 
-  public abstract function readBool($in, &$bool);
+  public abstract function readBool(&$bool);
   
-  public abstract function readByte($in, &$byte);
+  public abstract function readByte(&$byte);
   
-  public abstract function readI16($in, &$i16);
+  public abstract function readI16(&$i16);
 
-  public abstract function readI32($in, &$i32);
+  public abstract function readI32(&$i32);
 
-  public abstract function readI64($in, &$i64);
+  public abstract function readI64(&$i64);
 
-  public abstract function readDouble($in, &$dub);
+  public abstract function readDouble(&$dub);
 
-  public abstract function readString($in, &$str);
+  public abstract function readString(&$str);
 
   /**
    * The skip function is a utility to parse over unrecognized date without
    * causing corruption.
    *
-   * @param TTransport $in Input transport
    * @param TType $type What type is it
    */
-  public function skip($in, $type) {
+  public function skip($type) {
     switch ($type) {
     case TType::BOOL:
-      return $this->readBool($in, $bool);
+      return $this->readBool($bool);
     case TType::BYTE:
-      return $this->readByte($in, $byte);
+      return $this->readByte($byte);
     case TType::I16;
-      return $this->readI16($in, $i16);
+      return $this->readI16($i16);
     case TType::I32:
-      return $this->readI32($in, $i32);
+      return $this->readI32($i32);
     case TType::I64:
-      return $this->readI64($in, $i64);
+      return $this->readI64($i64);
     case TType::DOUBLE:
-      return $this->readDouble($in, $dub);
+      return $this->readDouble($dub);
     case TType::STRING:
-      return $this->readString($in, $str);
+      return $this->readString($str);
     case TType::STRUCT:
       {
-        $result = $this->readStructBegin($in, $name);
+        $result = $this->readStructBegin($name);
         while (true) {
-          $result += $this->readFieldBegin($in, $name, $ftype, $fid);
+          $result += $this->readFieldBegin($name, $ftype, $fid);
           if ($ftype == TType::STOP) {
             break;
           }
-          $result += $this->skip($in, $ftype);
-          $result += $this->readFieldEnd($in);
+          $result += $this->skip($ftype);
+          $result += $this->readFieldEnd();
         }
-        $result += $this->readStructEnd($in);
+        $result += $this->readStructEnd();
         return $result;
       }
     case TType::MAP:
       {
-        $result = $this->readMapBegin($in, $keyType, $valType, $size);
+        $result = $this->readMapBegin($keyType, $valType, $size);
         for ($i = 0; $i < $size; $i++) {
-          $result += $this->skip($in, $keyType);
-          $result += $this->skip($in, $valType);
+          $result += $this->skip($keyType);
+          $result += $this->skip($valType);
         }
-        $result += $this->readMapEnd($in);
+        $result += $this->readMapEnd();
         return $result;
       }
     case TType::SET:
       {
-        $result = $this->readSetBegin($in, $elemType, $size);
+        $result = $this->readSetBegin($elemType, $size);
         for ($i = 0; $i < $size; $i++) {
-          $result += $this->skip($in, $elemType);
+          $result += $this->skip($elemType);
         }
-        $result += $this->readSetEnd($in);
+        $result += $this->readSetEnd();
         return $result;
       }
     case TType::LST:
       {
-        $result = $this->readListBegin($in, $elemType, $size);
+        $result = $this->readListBegin($elemType, $size);
         for ($i = 0; $i < $size; $i++) {
-          $result += $this->skip($in, $elemType);
+          $result += $this->skip($elemType);
         }
-        $result += $this->readListEnd($in);
+        $result += $this->readListEnd();
         return $result;
       }
     default:
@@ -247,7 +306,7 @@
         while (true) {
           $ftype = 0;
           $fid = 0;
-          $data = $in->readAll(1);
+          $data = $itrans->readAll(1);
           $arr = unpack('c', $data);
           $ftype = $arr[1];
           if ($ftype == TType::STOP) {
@@ -309,4 +368,17 @@
   }
 }
 
+/**
+ * Protocol factory creates protocol objects from transports
+ */
+interface TProtocolFactory {
+  /**
+   * Build input and output protocols from the given transports.
+   *
+   * @return array Two elements, (iprot, oprot)
+   */
+  public function getIOProtocols($itrans, $otrans);
+}
+    
+
 ?>
diff --git a/lib/php/src/protocol/TType.php b/lib/php/src/protocol/TType.php
deleted file mode 100644
index aa930ce..0000000
--- a/lib/php/src/protocol/TType.php
+++ /dev/null
@@ -1,41 +0,0 @@
-<?php
-
-/**
- * Constants for Thrift data types.
- *
- * @package thrift.protocol
- * @author Mark Slee <mcslee@facebook.com>
- */
-
-/**
- * Data types that can be sent via Thrift
- */
-class TType {
-  const STOP   = 0;
-  const VOID   = 1;
-  const BOOL   = 2;
-  const BYTE   = 3;
-  const I08    = 3;
-  const DOUBLE = 4;
-  const I16    = 6;
-  const I32    = 8;
-  const I64    = 10;
-  const STRING = 11;
-  const UTF7   = 11;
-  const STRUCT = 12;
-  const MAP    = 13;
-  const SET    = 14;
-  const LST    = 15;    // N.B. cannot use LIST keyword in PHP!
-  const UTF8   = 16;
-  const UTF16  = 17;
-}
-
-/**
- * Message types for RPC
- */
-class TMessageType {
-  const CALL  = 1;
-  const REPLY = 2;
-}
-
-?>