PHP:解析不同文件/命名空间中的类型 [英] PHP: resolving types in different files/namespaces

查看:42
本文介绍了PHP:解析不同文件/命名空间中的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为用 C 编写的 Thrift 服务器编写 PHP 客户端.我正在按照 http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/.

I am trying to write a PHP client for a Thrift server written in C. I am following the tutorial given at http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/.

我对 PHP 非常陌生,我怀疑我缺少一些语言基础知识.

I am very new to PHP and, I suspect, am missing some of the language fundamentals.

有问题的代码是:

<?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’类"

I am getting an error "Class 'TSocket' not found in /var/www/foo/scores.php on line 22"

TSocket 在 Thrift/transport/TSocket.php 中定义.Thrift/transport/TSocket.php 文件(已删除注释)内容如下:

TSocket is defined in Thrift/transport/TSocket.php. The Thrift/transport/TSocket.php file (with comments removed) reads:

<?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');

然后我的代码(好吧,至少这一行)不再抛出错误.我很好奇:本教程的作者做了什么让这段代码在他的系统上运行,而我没有在我的系统上运行?

Then my code (well, this line, at least) no longer throws an error. I'm curious: what did the author of this tutorial do to get this code to work on his system that I'm not doing on my system?

其次,我尝试通过添加以下行来解决问题:

Secondly, I attempted to solve the problem by adding the line:

use Thrift\Transport;

在我创建 $socket 之前添加到我的文件中,但这并没有像我预期的那样解决问题.

to my file before I create $socket, but that didn't solve the problem as I'd expected it to.

如何说服 PHP 解析在另一个文件中定义的这种类型?(我必须解决几个文件中的几种类型.)

How do I convince PHP to resolve this type that's defined in another file? (There are several types in several files that I have to resolve.)

推荐答案

PHP 不会导入整个命名空间,您必须使用"要导入的每个命名空间类.而不是 use Thrift\Transport; write

PHP does not import an entire namespace, instead you must "use" each namespaced class you wish to import. Instead of use Thrift\Transport; write

use Thrift\Transport\TSocket;

使用命名空间:别名/导入.有没有可能这篇文章是在 Thrift 被命名空间之前写的?

This is described in more detail in the manual under Using Namespaces: Aliasing/Importing. Is it possible that the article was written before Thrift was namespaced?

这篇关于PHP:解析不同文件/命名空间中的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆