如何包含OOP中的"defiant randomdotorg"库在codeigniter中? [英] How do I include the OOP "defiant randomdotorg" library in codeigniter?

查看:64
本文介绍了如何包含OOP中的"defiant randomdotorg"库在codeigniter中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该类在这里(带有说明).

我已将其添加到application/libraries/randomorg

$this->load->library('RandomOrg\Random');添加到控制器的构造函数中时,出现以下错误.

When I add $this->load->library('RandomOrg\Random'); to my controller's constructor, I get the following error.

消息:找不到类"RandomOrg \ Client"

Message: Class 'RandomOrg\Client' not found

在此之前,我尝试添加各种文件(例如Client.php),但这无济于事.

I have tried adding the various files before that (such as Client.php), but that doesn't help.

这是怎么回事,我该如何解决?

What is going on and how do I fix it?

推荐答案

我知道我已经回答了这个问题,然后才找不到位置,也不知道要搜索什么.就是这样:Codeigniter只能加载单个 php文件库(不包括完全不同的驱动程序).要加载这种(命名空间的)库,您必须使用类似的内容:

I know I've answered this question before I just can't find where and don't know what to search. So here it is: Codeigniter can only load single php file libraries (excluding drivers which is a different thing entirely). To load this kindof library (namespaced) you have to use something like: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md (class example).

让我们将其命名为Autoloader_psr4并将其保存在库中(修改类声明以逐字匹配此名称(例如Autoloader_psr4).删除类中的名称空间声明,使其类似于:

Let's call it Autoloader_psr4 and save it in libraries (modify the class declaration to match this name verbatim (e.g. Autoloader_psr4). Remove the namespace declaration in the class so it looks like: https://pastebin.com/NU8Rbp7Y

让我们还将src/randomorg/中的所有文件移动到third_party中名为RandomOrg的文件夹中,例如application/third_party/RandomOrg.您的文件夹应该看起来像这里的内容: https://github.com/defiant /randomorg/tree/master/src/randomorg

Let's also move all the files in src/randomorg/ to just be in a folder in third_party called RandomOrg e.g. application/third_party/RandomOrg. Your folder should look like the contents here: https://github.com/defiant/randomorg/tree/master/src/randomorg

用法:

$this->load->library('autoloader_psr4');
$this->autoloader_psr4->register();
$this->autoloader_psr4->addNamespace('RandomOrg', APPPATH . 'third_party/RandomOrg');
$random = new \RandomOrg\Client(); // or whatever...

这篇关于如何包含OOP中的"defiant randomdotorg"库在codeigniter中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆