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

2023-10-16php开发问题
8

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17