复制 Codeigniters 加载函数

发布于 2024-10-24 16:45:51 字数 336 浏览 6 评论 0原文

我最近一直在玩 Codeigniter,看看能从中学到什么。我遇到了加载函数,想知道是否有人知道它是如何完成的。基本上,它看起来像这样:

$this->load->model('Model_name');
$this->Model_name->some_function();

现在 load 显然是一个类,并且创建了一个实例并调用 load。加载包含类“Model_name”并创建它的实例。但我无法解决的部分是,加载类如何创建一个名为“Model_name”的“类变量”以在代码的第二行中使用?我实际上将如何在 php.ini 中实现它?

谢谢。

I have recently been playing around with Codeigniter to see what I can learn from it. I came across the load function and was wondering if anyone knows how its done. Basically, it looks something like:

$this->load->model('Model_name');
$this->Model_name->some_function();

Now load is obviously a class and an instance is created and called load. And load includes the class "Model_name" and creates an instance of it. But the part I cant work out, is how does the load class create a "class variable" named "Model_name" to be used as in the second line of the code? And how would I actually go about implementing this in php.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落画卷 2024-10-31 16:45:51

该类的基本作用是记住所有创建的对象(例如 $this),然后通过引用将新创建的类分配为这些类中的变量。

function Load($className)
{
  $newClass = new $className();
  foreach($this->objects as &$object) //objects is array with created objects
    $object->$className = $newClass;
}

然而,它在后台做的事情远不止这些。您知道您可以打开“loader.php”然后阅读它的作用,对吧?

What the class basicly does is remembering all the created objects ($this for instance) and then assign the newly created class by reference as a variable in those classes.

function Load($className)
{
  $newClass = new $className();
  foreach($this->objects as &$object) //objects is array with created objects
    $object->$className = $newClass;
}

however, it does a lot more stuff in the background than that. You know you can just open 'loader.php' and then read what it does, right?

旧时浪漫 2024-10-31 16:45:51

这种事情适用于像 PHP 这样的解释语言。尽管想象这一点可能会非常令人困惑,特别是如果您有使用 C++、C# 等严格语言的经验。

其想法是,有一些 PHP 函数可以执行 PHP 代码,并且结果将在脚本的其他位置可见。

This kind of things work with interpreted languages like PHP. Though it can be very confusing to picture this, specially if you are experienced with strict languages such as C++, C# etc.

The idea is, there are PHP functions that can execute PHP code and the result will be visible elsewhere in the script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文