• <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 中使用没有命名空间的类

        时间:2023-10-16

          <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 中使用没有命名空间的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Yii2 中获取 JSON 格式的响应 下一篇:如何使用 Yii 框架 2.0 上传多个文件

                  相关文章

                      <bdo id='KxOJ4'></bdo><ul id='KxOJ4'></ul>
                  1. <tfoot id='KxOJ4'></tfoot>
                  2. <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>

                  3. <small id='KxOJ4'></small><noframes id='KxOJ4'>

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