问题描述
我已将其添加到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.
在此之前,我尝试添加各种文件(例如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 \RandomOrg\Client(); // or whatever...
这篇关于如何包含OOP中的"defiant randomdotorg"库在codeigniter中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!