Search code examples
phpcodeigniterthrift

when use apache thrift in codeigniter show me class not found


I want to use apache thrift in codeigniter libaries.
I have the following codes. I have checked the files and they exist.
php version is PHP 5.6.31
thrift verion is 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);

then it show me the error:

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

and I search this problem in google and ST, just this link this about , but it not solve my problem.

Please let me know what I did wrong.

Thanks.


Now the problem is solved, just use php require func to load the file.
But anyhow thanks long's help.
The code is blow:

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

Solution

  • when you use namespace ,you should use composer

    you can $config['composer_autoload'] = TRUE in the config/config.php file; this option is modified to TRUE, and the default is FALSE

    Attention here. If you modify the TRUE, then CI is automatically loaded application/vendor/autoload.php so, if your vendor directory in your project root directory, and index.php at the same level, then you can use $config['composer_autoload'] = realpath (APPPATH.'../vendor/ autoload.php'); the way to introduce Composer