理解CodeIgniter中的load_class [英] understanding the load_class in CodeIgniter

查看:136
本文介绍了理解CodeIgniter中的load_class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图理解CodeIgniter的框架结构,我刚刚开始,并想出了这个小小的误解。有人可以请帮助我在下面: -



1-为什么他们使用引用传递类的实例...我的意思是为什么不是一个简单的变量?



2-和为什么函数存储类的名称在数组,而不是一个字符串变量(请不要判断我的PHP的术语,因为它是最糟糕的)..?

  static $ _classes = array(); 
^^^^^^^这个云只是()或者我失去了一些东西

这里是函数,所以你不会去寻找它。

  function& load_class($ class,$ directory = 'libraries',$ prefix ='CI_')
{
static $ _classes = array();

//类是否存在?如果是这样,我们完成了...
if(isset($ _ classes [$ class]))
{
return $ _classes [$ class];
}

$ name = FALSE;

//在本地应用程序/ libraries文件夹中查找类
//然后在本地系统/ libraries文件夹
中foreach(array(APPPATH,BASEPATH)as $路径)
{
if(file_exists($ path。$ directory。'/'。$ class。'。php'))
{
$ name = $ prefix。$类;

if(class_exists($ name)=== FALSE)
{
require($ path。$ directory。'/'。
}

break;
}
}

//请求是类扩展吗?如果是这样,我们加载它
if(file_exists(APPPATH。$ directory。'/'。config_item('subclass_prefix')。$ class。'。php'))
{
$ name = config_item('subclass_prefix')。$ class;

if(class_exists($ name)=== FALSE)
{
require(APPPATH。$ directory。'/'config_item('subclass_prefix')。 '.php');
}
}

//我们找到了类吗?
if($ name === FALSE)
{
//注意:我们使用exit()而不是show_error()来避免
//自引用循环与Excptions类
exit('无法找到指定的类:'。$ class。'。php');
}

//跟踪我们刚刚加载的内容
is_loaded($ class);

$ _classes [$ class] = new $ name();
return $ _classes [$ class];
}


解决方案

c $ c> static 关键字 $ _ classes = array(); 。这使得 $ _ classes 数组保持它在对函数的多次调用之间的值。基本上,它们将它用作实例化类的本地缓存。为此,字符串不起作用。



查看更多关于static关键字



对于返回的引用,我认为是php4行李,CI是支持php4直到2.x.您可能会发现博客文章有助于了解从php4更改为php5。


im trying to understand the framework structure in CodeIgniter and i have just started and came up with this little misunderstanding .

so can somebody please help me under stand the following :-

1- why do they use a reference to pass an instance of the class ... i mean why not just a simple variable ?

2- and why does the function store the name of the class in an array instead of a "string variable" (please dont judge my php terms as im the worst at it ).. ?!

static $_classes = array();
                   ^^^^^^^ this cloud be just ("") or am i missing something

here is the function so you wont go looking for it .

function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
    {
        static $_classes = array();

        // Does the class exist?  If so, we're done...
        if (isset($_classes[$class]))
        {
            return $_classes[$class];
        }

        $name = FALSE;

        // Look for the class first in the local application/libraries folder
        // then in the native system/libraries folder
        foreach (array(APPPATH, BASEPATH) as $path)
        {
            if (file_exists($path.$directory.'/'.$class.'.php'))
            {
                $name = $prefix.$class;

                if (class_exists($name) === FALSE)
                {
                    require($path.$directory.'/'.$class.'.php');
                }

                break;
            }
        }

        // Is the request a class extension?  If so we load it too
        if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
        {
            $name = config_item('subclass_prefix').$class;

            if (class_exists($name) === FALSE)
            {
                require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
            }
        }

        // Did we find the class?
        if ($name === FALSE)
        {
            // Note: We use exit() rather then show_error() in order to avoid a
            // self-referencing loop with the Excptions class
            exit('Unable to locate the specified class: '.$class.'.php');
        }

        // Keep track of what we just loaded
        is_loaded($class);

        $_classes[$class] = new $name();
        return $_classes[$class];
    }

解决方案

The key is the static keyword before $_classes = array();. This makes the $_classes array hold it's value in between multiple calls to the function. Basically they use it as a local cache for the instantiated classes. For this purpose a string won't work.

See more about the static keyword in the manual.

As for the reference returning, i think that's php4 baggage, CI was supported on php4 until 2.x. You might find this blogpost helpful to see what was changed from php4 to php5.

这篇关于理解CodeIgniter中的load_class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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