【问题标题】:PHP: resolving types in different files/namespacesPHP:解析不同文件/命名空间中的类型
【发布时间】: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 解析在另一个文件中定义的这种类型? (我必须解决的几个文件中有几种类型。)

【问题讨论】:

标签: php thrift


【解决方案1】:

PHP 不会导入整个命名空间,相反,您必须“使用”您希望导入的每个命名空间类。而不是use Thrift\Transport;

use Thrift\Transport\TSocket;

这在Using Namespaces: Aliasing/Importing 下的手册中有更详细的描述。有没有可能这篇文章是在 Thrift 命名空间之前写的?

【讨论】:

    猜你喜欢
    • 2022-10-14
    • 1970-01-01
    • 2019-12-23
    • 2022-12-31
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    相关资源
    最近更新 更多