尝试使用作曲家时意外的“使用"(T_USE)

2022-01-14 00:00:00 oop namespaces php

所以,我正在尝试使用 coinbase API.我正在尝试一个简单的测试,看看我是否可以让它工作,但我遇到了各种作曲家错误.

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 CoinbaseWalletClient;
            use CoinbaseWalletConfiguration;

            $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.

在 Keks 类之外,我得到了

Outside of the Keks class, I get

致命错误:CoinbaseWalletConfiguration"类未在/home/content/61/11420661/html/beta/application/controllers/keks.php在第 15 行

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

在类内但在 index() 函数之外

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

致命错误:在第 4 行的 >/home/content/61/11420661/html/beta/application/controllers/keks.php 中找不到特征CoinbaseWalletClient"

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

我的 composer.json 可能有问题吗?

Is there something wrong in my composer.json maybe?

完整的控制器在这里:http://pastebin.com/4BjPP6YR

推荐答案

不能在使用的地方使用use".

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

use"关键字要么在类定义前面,用于将其他类/接口/特征导入到它自己的命名空间中,要么在类内部(但不在方法内部)以向类添加特征.

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 DifferentClass; // use can go here

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

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

相关文章