0

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);
code.g
  • 143
  • 1
  • 1
  • 14

1 Answers1

0

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

Virb
  • 1,639
  • 1
  • 16
  • 25
long
  • 86
  • 5
  • Is that to say,I must use composer to load the thrift? The project do't use composer, is there other ways? – code.g Nov 02 '17 at 07:42
  • Ci is not supported by namespace. If you don't want to use composer and want to use Ci, then don't use namespace. If you want to use namespaces, I suggest you use laravel – long Nov 02 '17 at 07:49
  • I see,CI is not supported by namespace.I think that, is all supported by namespace ,when php's version is above 5.3. – code.g Nov 02 '17 at 08:12
  • yes,when php's version is above 5.3. is all supported by namespace.but php how to know find those namespace,if you not include those file? – long Nov 02 '17 at 08:20
  • In the code,I try to use the thrift `ThriftClassLoader` to load the `MyServiceClient` for me.But it does't work. – code.g Nov 02 '17 at 08:24
  • if you want to use the thrift ThriftClassLoader to load the MyServiceClient You should use the CI method to load ThriftClassLoader, otherwise, CI doesn't know the way – long Nov 02 '17 at 08:25