我正在尝试为用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文件(删除了注释)读取:
<?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解析在另一个文件中定义的此类型? (我必须解决几个文件中的几种类型。)
答案 0 :(得分:2)
PHP不会导入整个命名空间,而是必须“使用”您要导入的每个命名空间类。而不是use Thrift\Transport;
写
use Thrift\Transport\TSocket;
Using Namespaces: Aliasing/Importing下的手册对此进行了详细介绍。文章是否可能在Thrift被命名空间之前编写?