• <i id='M8bMS'><tr id='M8bMS'><dt id='M8bMS'><q id='M8bMS'><span id='M8bMS'><b id='M8bMS'><form id='M8bMS'><ins id='M8bMS'></ins><ul id='M8bMS'></ul><sub id='M8bMS'></sub></form><legend id='M8bMS'></legend><bdo id='M8bMS'><pre id='M8bMS'><center id='M8bMS'></center></pre></bdo></b><th id='M8bMS'></th></span></q></dt></tr></i><div id='M8bMS'><tfoot id='M8bMS'></tfoot><dl id='M8bMS'><fieldset id='M8bMS'></fieldset></dl></div>
  • <small id='M8bMS'></small><noframes id='M8bMS'>

          <bdo id='M8bMS'></bdo><ul id='M8bMS'></ul>
        <tfoot id='M8bMS'></tfoot>
        <legend id='M8bMS'><style id='M8bMS'><dir id='M8bMS'><q id='M8bMS'></q></dir></style></legend>

        在 Yii2 中使用没有命名空间的类

        Using classes without namespace with Yii2(在 Yii2 中使用没有命名空间的类)

          <small id='zleAP'></small><noframes id='zleAP'>

            <bdo id='zleAP'></bdo><ul id='zleAP'></ul>
              <tbody id='zleAP'></tbody>
              <i id='zleAP'><tr id='zleAP'><dt id='zleAP'><q id='zleAP'><span id='zleAP'><b id='zleAP'><form id='zleAP'><ins id='zleAP'></ins><ul id='zleAP'></ul><sub id='zleAP'></sub></form><legend id='zleAP'></legend><bdo id='zleAP'><pre id='zleAP'><center id='zleAP'></center></pre></bdo></b><th id='zleAP'></th></span></q></dt></tr></i><div id='zleAP'><tfoot id='zleAP'></tfoot><dl id='zleAP'><fieldset id='zleAP'></fieldset></dl></div>
            • <tfoot id='zleAP'></tfoot><legend id='zleAP'><style id='zleAP'><dir id='zleAP'><q id='zleAP'></q></dir></style></legend>
                1. 本文介绍了在 Yii2 中使用没有命名空间的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在 Yii2 中使用 Checkout SDK 但因为这个库不支持 PSR-4标准(命名空间)我很难集成它.我怎样才能将这个库用于我的目的?

                  I want to use Checkout SDK with Yii2 but since this library does not support PSR-4 standards (namespaces) I am having trouble to integrate it. How can I use this library for my purpose?

                  编辑

                  建议我尝试使用类作为

                  As suggested I tried to use class as

                  $sale = new Twocheckout_Sale();
                  

                  但我仍然无法让它工作.

                  but still I am unable to make it work.

                  推荐答案

                  当类没有命名空间时,意味着它在根命名空间中.

                  When the class does not have namespace it means it's in the root namespace.

                  选项 1:

                  use Twocheckout;
                  
                  ...
                  
                  Twocheckout::format('json');
                  

                  选项 2:

                  Twocheckout::format('json');
                  

                  例如,PHPExcel 扩展也没有命名空间,类似的问题在 官方论坛.

                  For example, PHPExcel extension also doesn't have namespaces, similar question was answered on official forum.

                  相关问题:

                  将没有命名空间的类导入到命名空间的类

                  如何使用root"php的命名空间?

                  官方 PHP 文档:

                  http://php.net/manual/en/language.namespaces.fallback.php

                  更新:

                  但是 PHPExcel 有自己的自动加载器,而 2Checkout 没有.通过要求一个主要的抽象类来包含所有类.它甚至在官方 readme 中提到:

                  But PHPExcel has own autoloader, while 2Checkout does not. All classes are included by requiring one main abstract class. It's even mentioned in official readme:

                  require_once("/path/to/2checkout-php/lib/Twocheckout.php");
                  

                  所以你需要在使用库类之前手动包含它.可以借助别名来避免写全路径.

                  So you need to manually include it before using library classes. It can be done with help of alias to avoid writing full path.

                  use Yii;
                  ...
                  $path = Yii::getAlias("@vendor/2checkout/2checkout-php/lib/Twocheckout.php");
                  require_once($path);
                  $sale = new Twocheckout_Sale();
                  

                  在一个地方使用是可以的,但是如果要在很多地方使用,最好在入口脚本index.php中require它:

                  For usage in one place it's OK, but if it will be used in many places of application, it's better to require it in entry script index.php:

                  require(__DIR__ . '/../../vendor/autoload.php');
                  
                  require(__DIR__ . '/../../vendor/2checkout/2checkout-php/lib/Twocheckout.php');
                  
                  require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
                  require(__DIR__ . '/../../common/config/bootstrap.php');
                  require(__DIR__ . '/../config/bootstrap.php');
                  

                  我还建议阅读官方文档中关于 使用下载的库,根据特定的库,您可以使用更多选项.

                  I also recommend to read tips in official documentatiton about using downloaded libraries, there are more options you can use depending on the specific library.

                  这篇关于在 Yii2 中使用没有命名空间的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                      <bdo id='KxOJ4'></bdo><ul id='KxOJ4'></ul>
                    • <tfoot id='KxOJ4'></tfoot>
                      • <i id='KxOJ4'><tr id='KxOJ4'><dt id='KxOJ4'><q id='KxOJ4'><span id='KxOJ4'><b id='KxOJ4'><form id='KxOJ4'><ins id='KxOJ4'></ins><ul id='KxOJ4'></ul><sub id='KxOJ4'></sub></form><legend id='KxOJ4'></legend><bdo id='KxOJ4'><pre id='KxOJ4'><center id='KxOJ4'></center></pre></bdo></b><th id='KxOJ4'></th></span></q></dt></tr></i><div id='KxOJ4'><tfoot id='KxOJ4'></tfoot><dl id='KxOJ4'><fieldset id='KxOJ4'></fieldset></dl></div>

                        • <small id='KxOJ4'></small><noframes id='KxOJ4'>

                          <legend id='KxOJ4'><style id='KxOJ4'><dir id='KxOJ4'><q id='KxOJ4'></q></dir></style></legend>
                            <tbody id='KxOJ4'></tbody>