Tim Behrendsen created THRIFT-1903: -------------------------------------- Summary: PHP namespaces cause binary protocols to not be used Key: THRIFT-1903 URL: https://issues.apache.org/jira/browse/THRIFT-1903 Project: Thrift Issue Type: Bug Components: PHP - Compiler, PHP - Library Affects Versions: 0.9 Environment: Centos 6.4 Reporter: Tim Behrendsen
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'; --------------------------------------------------------------------- -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators For more information on JIRA, see: http://www.atlassian.com/software/jira