【发布时间】:2013-04-22 16:27:26
【问题描述】:
我正在尝试为用 C 编写的 Thrift 服务器编写一个 PHP 客户端。我正在按照http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/ 提供的教程进行操作。
我对 PHP 非常陌生,我怀疑我缺少一些语言基础知识。
有问题的代码是:
<?php
// Setup the path to the thrift library folder
$GLOBALS['THRIFT_ROOT'] = 'Thrift';
// Load up all the thrift stuff
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
// Load the package that we autogenerated for this tutorial
require_once $GLOBALS['THRIFT_ROOT'].'/packages/encabulationgame/EncabulationGame.php';
// Several things might go wrong
try {
// Create a thrift connection (Boiler plate)
$socket = new TSocket('localhost', '65123'); <--error is here
我收到一个错误“第 22 行的 /var/www/foo/scores.php 中找不到类 'TSocket'”
TSocket 在 Thrift/transport/TSocket.php 中定义。 Thrift/transport/TSocket.php 文件(移除了 cmets)内容如下:
<?php
namespace Thrift\Transport;
use Thrift\Transport\TTransport;
use Thrift\Exception\TException;
use Thrift\Exception\TTransportException;
use Thrift\Factory\TStringFuncFactory;
/**
* Sockets implementation of the TTransport interface.
*
* @package thrift.transport
*/
class TSocket extends TTransport {
如果我将代码更改为:
$socket = new Thrift\Transport\TSocket('localhost', '65123');
然后我的代码(嗯,至少这一行)不再抛出错误。我很好奇:本教程的作者做了什么让这段代码在他的系统上运行而我没有在我的系统上运行?
其次,我尝试通过添加以下行来解决问题:
use Thrift\Transport;
在我创建 $socket 之前添加到我的文件中,但这并没有像我预期的那样解决问题。
如何说服 PHP 解析在另一个文件中定义的这种类型? (我必须解决的几个文件中有几种类型。)
【问题讨论】:
-
嘿,看看我在 github 上的 java-server/php-client thrift 应用程序:github.com/tkoomzaaskz/wealthy-laughing-duck。在那里,您可能会看到命名空间 PHP 如何使用命名空间与 thrift 服务器进行通信:github.com/tkoomzaaskz/wealthy-laughing-duck/tree/master/src/…