unexpected 'use' (T_USE) when trying to use composer
Asked Answered
B

3

38

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

Barcellona answered 26/10, 2015 at 10:4 Comment(5)
Which version of php do you use? In case of doubt try <?php echo phpversion();Sniggle
Then please run <?php echo phpversion(); to get the version.Sniggle
oh and by the way: The actual error message is parse error: syntax error, unexpected 'use' (T_USE) or is it something else? (If it is: what is before the code snippet you've posted?)Sniggle
The exact message is at the end of this, the code is exactly what I've shown in the pastbin link "Parse error: syntax error, unexpected 'use' (T_USE) in /home/content/61/11420661/html/beta/application/controllers/keks.php on line 9"Barcellona
I like this answer which includes link to docs: https://mcmap.net/q/48402/-php-class-phpmailer-unexpected-39-use-39-t_useDetinue
W
87

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
  }
}
Wadi answered 26/10, 2015 at 21:11 Comment(4)
This is wrong. use (in this case) has nothing to do with classes, and it doesn't need to be "in front of a class" at all.Julianajuliane
@Julianajuliane Look at the pastebin provided in the question, and you'll admit that this answer is reasonably accurate enough in this context.Wadi
In my case taking use statement to the top of the page solve the problem.Heredia
Or, as we say in Joisey, "Youze can't use "use" the way youze is using it."Thereunder
F
17

I'm using CodeIgniter when I try to use "use" keyword it's throwing the error within a method.

I just moved it above the class declaration.

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

  }
?>

It's working fine.

Foolproof answered 2/6, 2017 at 4:55 Comment(0)
G
4

You can also -

public function index() {
    ...
    $configuration = Coinbase\Wallet\Configuration::apiKey($apiKey, $apiSecret);
    $client = Coinbase\Wallet\Client::create($configuration);
    ...
}

If you wish to use class inside function and not declare it globally.

Goffer answered 3/8, 2021 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.