无需在 codeigniter 中扩展类/库

发布于 2024-08-08 08:51:12 字数 479 浏览 5 评论 0原文

我想检查一下我对 codeigniter 的假设是否正确?

当我们试图在核心中包含更多功能时,我们通常会扩展一个类,例如 MY_Controller 扩展 Controller、MY_Model 扩展 Model 等...

但是,例如,如果我们在结帐库中检索一些结帐信息(例如,product_id) ),我们只需 $this->load->library('product_lib',array('product_id'=>$product_id)) 就可以轻松 $this->product_lib- >product_name 等...来自结帐库,对吗?

$this->load 东西相当于“硬代码”签出库,用于扩展product_lib(class checkout_lib extends Product_lib),以便能够使用product_lib中的任何方法/变量。

请赐教。

I would like to check if my assumption about codeigniter is right ?

We would normally extend a class when we are trying to include more functionality to the core, such as MY_Controller extends Controller, MY_Model extends Model etc...

But for example, if we are in the checkout library retrieving some checkout info(eg, product_id), we can just $this->load->library('product_lib',array('product_id'=>$product_id)) and we can easily $this->product_lib->product_name etc... from the checkout library right?

The $this->load thing is kind of equivalent to "hard code" checkout library to extend product_lib(class checkout_lib extends product_lib) to be able to use whatever methods/variables there is in the product_lib.

Please enlighten me.

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

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

发布评论

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

评论(2

海螺姑娘 2024-08-15 08:51:12

在 CodeIgniter 中 $this->load 就像有一个资源管理器(例如,resourceManager->load("path/to/file")),它负责加载库,并传递任何您指定的参数等,可以轻松地让您快速开始使用它。

因此,如果您的product_lib中有一个名为product_name的变量,那么调用$this->product_lib->product_name将访问该变量。

实际上,它只是将库放入一个数组中,以库名称作为键,以库实例作为值,因此调用 $this->product_lib 实际上是调用类似于 $ 的东西loadLibraries['product_lib'] 并返回实例。

我希望能回答您的问题,我很累,可能无法理解您的问题。

In CodeIgniter $this->load is like having a resource manager (e.g. resourceManager->load("path/to/file")) and it takes care of loading the library, and passing any arguments you specify and such, easily allowing you to quickly get to using it.

So if you have a variable named product_name in your product_lib then yes calling $this->product_lib->product_name will be accessing that variable.

Really it just places the library into an array with the library name as the key and the instance of the library as the value so calling $this->product_lib is really calling something similar to $loadedLibraries['product_lib'] and returning the instance.

I hope that answers what you are asking, I'm quite tired and could have miss understood you question.

怀中猫帐中妖 2024-08-15 08:51:12

我认为您误解了 OO 范式和 CI 的工作方式。

$this->load 与实例化库/模型的对象或加载帮助程序文件相同。 CI 有某种管理来查看助手/库/模型是否已经上传。

另一方面,extends用于定义类时,告诉PHP该类将继承父类的属性和方法。类是它将生成的对象的蓝图。

也许你可以先从理解 OO 概念开始。您可以阅读本文作为开始,并查看其中使用的参考。

I think you misunderstood the OO paradigm and the way CI work.

$this->load is same with instantiate an object of the library/model, or load the helper file. CI have some sort of management to see if the helper/library/model already uploaded or not.

In other hand, the extends is used when defining a class, to tell PHP that the class will be inherit the parent class properties and method. A class is a blue print of object it will produce.

Maybe you can start by understanding the OO concept first. You can read this as a start, and see the reference used there.

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