在codeigniter中使用apache thrift时显​​示我找不到类

时间:2017-11-02 07:24:27

标签: php codeigniter thrift

我想在codeigniter库中使用apache thrift 我有以下代码。我检查了文件,它们存在。
php版本为PHP 5.6.31
thrift verion是0.10.*

use Thrift\Transport\TSocket;
use Thrift\Transport\TBufferedTransport;
use Thrift\Transport\THttpClient;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Exception\TException;
use Thrift\ClassLoader\ThriftClassLoader;
use \test\MyServiceClient;

class Cds2thriftclass extends Rootclass {
    private $_thrift_uri = "";
    private $_thrift_port = "";
    private $_timeout = 5000;

    private $transport;
    private $protocol;
    private $client;

    public function __construct() {
        parent::__construct();
        $this->_thrift_uri = 127.0.0.1;
        $this->_thrift_port = 8899;

        $THRIFT_ROOT = APPPATH . 'libraries/thrift10';
        require_once $THRIFT_ROOT.'/Thrift/ClassLoader/ThriftClassLoader.php';

        $GEN_DIR = $THRIFT_ROOT . '/Thrift/packages';

        $loader = new ThriftClassLoader();
        $loader->registerNamespace('Thrift', $THRIFT_ROOT);
        $loader->registerDefinition('test', $GEN_DIR);
        $loader->register();

        $this->transport = new TSocket($this->_thrift_uri, $this->_thrift_port);
        $this->transport->setRecvTimeout($this->_timeout);
        $this->transport = new TBufferedTransport($this->transport, 1024, 512);
        $this->protocol = new TBinaryProtocol($this->transport);
        $this->client = new \test\MyServiceClient($this->protocol);

然后它显示错误:

Class 'test\MyServiceClient' not found in /apps/blog/application/libraries/mythriftclass.php 

我在谷歌和ST搜索这个问题,只是这个 link 这个,但它没有解决我的问题。

请让我知道我做错了什么。

感谢。

现在问题解决了,只需使用php require func加载文件即可 但无论如何,感谢长期的帮助 代码很好:

    $THRIFT_ROOT = APPPATH . 'libraries/thrift10';
    require_once $THRIFT_ROOT.'/Thrift/ClassLoader/ThriftClassLoader.php';

    $GEN_DIR = $THRIFT_ROOT . '/Thrift/packages';

    $loader = new ThriftClassLoader();
    $loader->registerNamespace('Thrift', $THRIFT_ROOT);
    $loader->registerDefinition('test', $GEN_DIR);
    $loader->register();

    $this->transport = new TSocket('127.0.0.1', 8899);
    $this->transport->setRecvTimeout($this->_timeout);
    $this->transport = new TBufferedTransport($this->transport, 1024, 512);
    $this->protocol = new TBinaryProtocol($this->transport);

    require_once $THRIFT_ROOT . '/Thrift/gen-php/test/Types.php';
    require_once $THRIFT_ROOT . '/Thrift/gen-php/test/Cds2APIService.php';

    $this->client = new MyServiceClient($this->protocol);

1 个答案:

答案 0 :(得分:0)

使用命名空间时,应使用composer

您可以在$config['composer_autoload'] = TRUE文件中config/config.php;此选项被修改为TRUE,默认值为FALSE

注意这里。如果您修改TRUE,则会自动加载CI application/vendor/autoload.php,因此,如果项目根目录中的供应商目录和同一级别的index.php,那么您可以使用$config['composer_autoload'] = realpath (APPPATH.'../vendor/ autoload.php');的方式介绍作曲家