简体   繁体   English

无法使用Composer安装Omnipay库

[英]Trouble installing Omnipay library with Composer

I'm trying to use the Omnipay API with the Pin gateway but having issues. 我正在尝试将Omnipay API与Pin网关一起使用但存在问题。

I've run composer.phar locally with this in the composer.json file: 我在composer.json文件中使用this运行composer.phar:

   {
    "require": {
        "omnipay/omnipay": "~2.0"
    }
    }

The composer "vendor" folder now resides at "classes/libs/vendor" on my site. 作曲家“vendor”文件夹现在位于我的网站上的“classes / libs / vendor”中。

I have this payment class: 我有这个付款类:

class Payment extends BasicClass {
    public function charge() {
        require "libs/vendor/autoload.php";

        use Omnipay\Omnipay;

        $gateway = GatewayFactory::create('Pin');
        $gateway->setSecretKey($this->config->secretKey);
    }
}

When calling: 致电时:

$topup = new Payment();
$topup->charge();

I get a parse error, PHP takes issue with my use Omnipay\\Omnipay line. 我得到一个解析错误,PHP在use Omnipay\\Omnipay行时遇到问题。

Very confused as all Omnipay documentation seems to use this syntax. 非常困惑,因为所有Omnipay文档似乎都使用这种语法。

I've tried the require and use lines outside the class, but that did not help. 我已经尝试过要求并使用课外线,但这没有用。

Thanks for everyone's help. 谢谢大家的帮助。

You have to use the use operator outside the class definition. 您必须在类定义之外使用use运算符。

From PHP documentation : 从PHP文档:

Scoping rules for importing 导入的范围规则

The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. use关键字必须在文件的最外层范围 (全局范围)或命名空间内声明中声明。 This is because the importing is done at compile time and not runtime, so it cannot be block scoped. 这是因为导入是在编译时完成的,而不是运行时,所以它不能是块作用域。

http://php.net/manual/en/language.namespaces.importing.php#language.namespaces.importing.scope http://php.net/manual/en/language.namespaces.importing.php#language.namespaces.importing.scope

You are misusing the Composer autoloader. 您正在滥用Composer自动加载器。 You are supposed to include the autoloader as one of the very first files in every request, possibly before loading any configuration files, or starting a session. 您应该将自动加载器作为每个请求中的第一个文件之一,可能在加载任何配置文件或启动会话之前。

As it is currently coded, you would add another instance of the Composer autoloader whenever you make a call to that charge method. 由于它是当前编码的,因此每当您调用该charge方法时,都会添加Composer自动加载器的另一个实例。 Calling charge three times in one request (for example when billing three customers in a loop) will add three autoloaders on top of each other. 在一个请求中调用三次charge (例如,在循环中为三个客户计费时)将在彼此之上添加三个自动加载器。 This sounds like a bad idea. 这听起来像个坏主意。

Please refer to the documentation at http://getcomposer.org/doc/01-basic-usage.md#autoloading to see how the composer autoloader should be included. 请参阅http://getcomposer.org/doc/01-basic-usage.md#autoloading上的文档,了解如何包含composer自动加载器。

Especially pay attention to the paragraph stating that Composer could also autoload your own classes if you define it in your composer.json file. 特别要注意段落,如果你在composer.json文件中定义它,Composer也可以自动加载你自己的类。 This is really a nice feature, you'd never again need to require the classes you are about to use. 这真的是一个很好的功能,你再也不需要要使用你要使用的类了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM