当在 codeigniter 中使用 apache thrift 告诉我 class 未找到

when use apache thrift in codeigniter show me class not found

我想在 codeigniter 库中使用 apache thrift。
我有以下代码。我检查了文件,它们存在。
php 版本为 PHP 5.6.31
节俭版本是 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 

我在google和ST上搜索这个问题,就这个 link 这个问题,但是并没有解决我的问题。

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

谢谢。


现在问题已解决,只需使用php require func 加载文件即可。
不过还是感谢long的帮助。
代码如下:

    $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);

当你使用命名空间时,你应该使用 composer

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

注意这里。如果你修改为TRUE,那么CI会自动加载application/vendor/autoload.php 所以,如果你的vendor目录在你的项目根目录下,和index.php在同一层级,那么你可以使用$config['composer_autoload'] = realpath (APPPATH.'../vendor/ autoload.php'); Composer的介绍方式