unexpected 'use' (T_USE) when trying to use composer

百般思念 提交于 2019-12-28 13:47:33

问题


So, I am trying to use the coinbase API. I'm attempting a simple test to see if I can make it work, but I'm getting various composer errors.

Currently, I am getting unexpected t 'use' for this code:

            use Coinbase\Wallet\Client;
            use Coinbase\Wallet\Configuration;

            $apiKey = 'public';
            $apiSecret = 'private';
            $configuration = Configuration::apiKey($apiKey, $apiSecret);
            $client = Client::create($configuration);
            $spotPrice = $client->getSpotPrice();
            echo $spotPrice;

So, are my use statements in the wrong place? Ive tried them outside the index function and outside the class. Both yield completely different sets of results than this.

Outside of the Keks class, I get

Fatal error: Class 'Coinbase\Wallet\Configuration' not found in /home/content/61/11420661/html/beta/application/controllers/keks.php on line 15

And inside the class but outside the index() function I get

Fatal error: Trait 'Coinbase\Wallet\Client' not found in >/home/content/61/11420661/html/beta/application/controllers/keks.php on line 4

Is there something wrong in my composer.json maybe?

The full controller is here: http://pastebin.com/4BjPP6YR


回答1:


You cannot use "use" where you are using it.

The "use" keyword is either in front of a class definition to import other classes/interfaces/traits into it's own namespace, or it is inside the class (but not inside a method) to add traits to the class.

<?php
namespace Foo;

use Different\Class; // use can go here

class Bar {
  use TraitCode; // use can go here

  public function baz() {
    $this->traitFunction('etc');
    // use CANNOT go here
  }
}



回答2:


I'm using codeigniter when i try to use "use" keyword its throwing error within a method.

SO i just moved it to above class declaration.

<?php
  defined('BASEPATH') OR exit('No direct script access allowed');
  use Auth0\SDK\Auth0;

  class Home extends CI_Controller {

  }
?>

Its working fine.




回答3:


Errors

"Currently, I am getting unexpected t 'use' for this code " Or

parse error: syntax error, unexpected 'use' (T_USE)

Well I found such issue and wish to tell you I was using While loop I mean calling data from the database so actually whenever you see any " T " its mean you are Repeating the same script so I was doing the same and when to put it outside the while loop it worked for me It's my First Post here Jamal shah (From PAKISTAN)



来源:https://stackoverflow.com/questions/33342994/unexpected-use-t-use-when-trying-to-use-composer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!