问题描述
是否可以从代码igniter中的库中加载库?
Is it possible to load an library from within a library in code igniter?
如果我这样做
$ this-> validator = $ this-> CI-> load-> library('validators /'.$ params ['validator']);
从另一个库中$ this-> validator为NULL。
from within another library $this->validator is NULL.
为什么会这样?
推荐答案
查看CI_Loader类对您引用的library()方法的签名:
Check out the CI_Loader class's signature for the library() method you refer to:
/**
* Class Loader
*
* This function lets users load and instantiate classes.
* It is designed to be called from a user's app controllers.
*
* @access public
* @param string the name of the class
* @param mixed the optional parameters
* @param string an optional object name
* @return void
*/
function library($library = '', $params = NULL, $object_name = NULL)
{
它返回void,所以当然你设置的返回值为null。我想你对该方法的目的感到困惑。它加载库并将其附加到codeigniter超级对象,以便您可以引用它:
It returns void, so of course whatever you set the return value to will be null. I think you're confused about the purpose of that method. Its to load the library and attach it to the codeigniter super-object, so that you can reference it as:
$this->CI->[library name]
在您的情况下,到新加载的库(一些特定的验证器库,我基于你的代码片段猜测):
In your case, you'll just want to refer to the newly-loaded library (some specific validator library I'm guessing based on your code snippet) in the usual way:
$this->CI->[newly loaded super awesome validator library]
这篇关于代码点火器从库中加载库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!