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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33342994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:27:27  来源:igfitidea点击:

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

phpoopnamespaces

提问by Dan Smith

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.

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

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.

那么,我的 use 语句是在错误的地方吗?我已经在索引函数之外和课堂之外尝试过它们。两者都产生了与这完全不同的结果集。

Outside of the Keks class, I get

在 Keks 课之外,我得到

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

致命错误:在第 15 行的 /home/content/61/11420661/html/beta/application/controllers/keks.php 中找不到“Coinbase\Wallet\Configuration”类

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

在课堂内但在 index() 函数之外我得到

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

致命错误:在第 4 行的 >/home/content/61/11420661/html/beta/application/controllers/keks.php 中找不到 Trait 'Coinbase\Wallet\Client'

Is there something wrong in my composer.json maybe?

我的 composer.json 有什么问题吗?

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

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

回答by Sven

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.

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

<?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
  }
}

回答by user2215155

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

当我尝试在方法中使用“use”关键字时,我正在使用 codeigniter。

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.

它的工作正常。

回答by jamalshah

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 Whileloop 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)

好吧,我发现了这样的问题,并希望告诉您我正在使用While循环,我的意思是从数据库中调用数据,因此实际上每当您看到任何“T”时,这意味着您正在重复相同的脚本,因此我正在执行相同的操作以及何时放置它在 while 循环之外它对我有用 这是我在这里的第一篇文章 Jamal shah(来自巴基斯坦)