Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-1903

PHP namespaces cause binary protocols to not be used

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 0.9
    • 0.9.1
    • None
    • Centos 6.4

    Description

      Currently the compiled code produces the following check for binary capability (slightly different in other places, but same idea):

          $bin_accel = ($this->output_ instanceof TProtocol::$TBINARYPROTOCOLACCELERATED) && function_exists('thrift_protocol_write_binary');
      

      The TProtocol::$TBINARYPROTOCOLACCELERATED variable contains 'TBinaryProtocolAccelerated'. This check always fails because when the protocol class is instantiated like:

          use Thrift\Protocol\TBinaryProtocolAccelerated;
          $protocol = new TBinaryProtocolAccelerated($transport);
      

      (as shown in the current PHP example), the protocol class is instantiated as 'Thrift\Protocol\TBinaryProtocolAccelerated'. In the standard compiled class, TBinaryProtocolAccelerated's namespace is not use'd, so the instanceof fails.

      EXAMPLE PROGRAM
      Based on PHP tutorial, only needs compiled code. Does not make a
      connection and demonstrates the instanceof failure.
      --------------------------------------------------------------------

      namespace tutorial\php;
      
      define('THRIFT_ROOT', '/home/tim/projects/inquisic/thrift-0.9.0/lib/php/lib');
      require_once(THRIFT_ROOT . '/Thrift/ClassLoader/ThriftClassLoader.php');
      
      use Thrift\ClassLoader\ThriftClassLoader;
      $GEN_DIR = '/home/tim/public_html/thrift0.9/gen-php';
      
      $loader = new ThriftClassLoader();
      $loader->registerNamespace('Thrift', THRIFT_ROOT);
      $loader->registerDefinition('shared', $GEN_DIR);
      $loader->registerDefinition('tutorial', $GEN_DIR);
      $loader->register();
      
      use Thrift\Protocol\TBinaryProtocol;
      use Thrift\Protocol\TBinaryProtocolAccelerated;
      use Thrift\Transport\THttpClient;
      use Thrift\Transport\TBufferedTransport;
      
      $socket = new THttpClient('localhost', 8383, '/~tim/thrift0.9/PhpServer.php');
      $transport = new TBufferedTransport($socket, 1024, 1024);
      $protocol = new TBinaryProtocolAccelerated($transport);
      
      // Reset namespace
      namespace tutorial\php;
      
      use Thrift\Protocol\TProtocol;
      use Thrift\Protocol\TBinaryProtocol;
      
        print "protocol class = " . get_class($protocol) . "\n";
        print "check class = " . TProtocol::$TBINARYPROTOCOLACCELERATED . "\n";
        if ($protocol instanceof TProtocol::$TBINARYPROTOCOLACCELERATED)
          print "Standard: INSTANCE!\n";
        else
          print "Standard: NOT INSTANCE!\n";
      
        $good_class = '\Thrift\Protocol\TBinaryProtocolAccelerated';
        if ($protocol instanceof $good_class)
          print "Good: INSTANCE!\n";
        else
          print "Good: NOT INSTANCE!\n";
      

      ---------------------------------------------------------------------

      PROGRAM OUTPUT:
      ---------------------------------------------------------------------

      protocol class = Thrift\Protocol\TBinaryProtocolAccelerated
      check class = TBinaryProtocolAccelerated
      Standard: NOT INSTANCE!
      Good: INSTANCE!
      

      ---------------------------------------------------------------------

      WORKAROUND:
      Note that leading backslash is required. Just having 'Thrift\Protocol\...' etc will not work.
      ---------------------------------------------------------------------

      Changed line 37 in TProtocol.php to:
      
      static $TBINARYPROTOCOLACCELERATED = '\Thrift\Protocol\TBinaryProtocolAccelerated';
      

      ---------------------------------------------------------------------

      Attachments

        1. THRIFT-1903-v1.txt
          3 kB
          Tom Hobbs

        Activity

          People

            thobbs Tom Hobbs
            siliconengine Tim Behrendsen
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: