thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nevill <>
Subject A bug in PHP library ?
Date Wed, 20 Jul 2011 16:45:25 GMT
Hi all,

I am new to thrift.
I play a bit with it on PHP.

First, I create a struct like this

struct Book {
  1: i32 isbn,
  2: string name

Then, generate the Book class with thrift 0.6.1

Next, I use the script to test.

$GLOBALS['THRIFT_ROOT'] = __DIR__ . '/lib/vendor/thrift';
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinarySerializer.php';

require_once __DIR__ . '/gen-php/book/book_types.php';

$aBook = new Book(array('isbn' => 123456, 'name' => 'Programming PHP'));

$bin = TBinarySerializer::serialize($aBook);

$theBook = TBinarySerializer::deserialize($bin, 'Book');


echo "\n";


Then I get this error.

PHP Fatal error:  Uncaught exception 'TTransportException' with message
'TMemoryBuffer: Could not read 1 bytes from buffer.' in

After spending some hours on reading source code, transport/*, protocol/*, I
find it's because when using TBufferedTransport to serialize to binary, it
doesn't write to the underlying transport TMemoryBuffer at the end of the

So, I fix it like this in TBinarySerializer.php

  public static function serialize($object) {
    $transport = new TMemoryBuffer();
    $protocol = new TBinaryProtocolAccelerated($transport);
    if (function_exists('thrift_protocol_write_binary')) {
    } else {
      $protocol->flush();   // <----------------------------------- Here is
the fix
    return $transport->getBuffer();

Then, everything works as I expected.


My question is

Do I find a bug or I use the library in a wrong way ?

Best Regards
- Nevill

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message