PHP:解析不同文件/命名空间中的类型


PHP: resolving types in different files/namespaces

我正在尝试为用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

我得到一个错误"类'TSocket'没有找到/var/www/foo/scores.php在第22行"

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解析在另一个文件中定义的类型?

PHP不导入整个命名空间,相反,您必须"使用"希望导入的每个命名空间类。代替use Thrift'Transport; write

use Thrift'Transport'TSocket;

手册中使用命名空间:别名/导入有更详细的描述。有没有可能这篇文章是在Thrift被命名空间之前写的?