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

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

问题描述

课程在这里(有说明).

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

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

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

消息:找不到类RandomOrgClient"

Message: Class 'RandomOrgClient' 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 文件库(不包括完全不同的驱动程序).要加载这种库(命名空间),您必须使用以下内容:https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md(类示例).

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).删除类中的命名空间声明,以便看起来像:https://pastebin.com/NU8Rbp7Y

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 RandomOrgClient(); // or whatever...

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

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