PHP:解析不同文件/名称空间中的类型
问题描述:
我正在尝试编写用C编写的Thrift服务器的PHP客户端。我正在按照http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/给出的教程。PHP:解析不同文件/名称空间中的类型
我对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级 'TSocket' 在/var/www/foo/scores.php找不到” 收到错误
TSocket是在Thrift/transport/TSocket.php中定义。节俭/运输/ 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;
我的文件之前,我创建$插座,但我预料它没有解决不了的问题。
我该如何说服PHP解决在另一个文件中定义的这种类型? (我必须解析几个文件中的几种类型。)
答
PHP不会导入整个名称空间,而是您必须“使用”您希望导入的每个命名空间类。代替use Thrift\Transport;
写
use Thrift\Transport\TSocket;
此进行更详细的说明书中Using Namespaces: Aliasing/Importing下所述。这篇文章是否有可能在Thrift命名空间之前写入?
嘿,看看我的github上的java-server/php-client thrift应用程序:https://github.com/tkoomzaaskz/wealthy-laughing-duck。您可能会看到名称空间PHP如何使用名称空间与节俭服务器进行通信:https://github.com/tkoomzaaskz/wealthy-laughing-duck/tree/master/src/main/php – ducin 2013-04-22 12:22:36