理解CodeIgniter中的load_class

rai*_*eys 3 php codeigniter

我试图了解CodeIgniter中的框架结构,我刚刚开始并想出了这个小小的误解.

那么有人可以帮助我站在下面: -

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

2-为什么函数将类的名称存储在数组而不是"字符串变量"(请不要判断我的php术语是最糟糕的)...?!

static $_classes = array();
                   ^^^^^^^ this cloud be just ("") or am i missing something
Run Code Online (Sandbox Code Playgroud)

这是功能,所以你不会去寻找它.

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];
    }
Run Code Online (Sandbox Code Playgroud)

com*_*857 7

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

请参阅手册中有关static关键字的更多信息.

至于参考返回,我认为这是php4行李,在php4上支持CI直到2.x. 您可能会发现此博客文章有助于查看从php4更改为php5的内容.