对 CodeIgniter 中的对象和类感到困惑吗?
我对 CodeIgniter 还很陌生,有一个问题。我对类、库和对象有点困惑。
CodeIgniter 是否用库(即 $this->load->library('some_library'); 替换了使用对象的正常 PHP 方式,即
?$var = new car();
) $this->some_library->some_function();
如果两者都有效,有区别吗?如果是这样,有什么区别以及何时使用其中一种?哪个更常见/合适?
我这样问是因为我创建了一个类,但我不确定实例化它的正确方式是什么。
提前致谢
I’m fairly new to CodeIgniter and have a question. I’m a bit confused about Classes, Libraries and Objects.
Does CodeIgniter replace the normal PHP way of usings objects i.e. $var = new car();
with libraries i.e. $this->load->library('some_library'); $this->some_library->some_function();
?
If both are valid, is there a difference? If so, what are the differences and when do I use one over the other? Which is more common/proper?
I am asking because I created a class, but I'm not certain what is the correct manner in which to instantiate it.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对 CodeIgnitier 不熟悉。但熟悉其他PHP框架。大多数框架都使用这种方式来提高性能,注册事物,执行某些事件,并使开发人员的事情变得更简单......
例如,如果您想在
library
目录中的某个位置创建类“car”您必须先包含该文件,然后才能创建该类的对象(多行代码,更多的错误空间)。该框架将创建类并在 1 行代码中包含相关文件(更简单、更安全)。框架方式也可以作为工厂。它不会重新创建对象,而是仅创建一次对象,每次再次调用该方法时,它都会返回对现有对象的引用。
当您使用框架时,更多的事情会在幕后发生。事情正在登记,等等......
I am not familiar with CodeIgnitier. But familiar with other PHP frameworks. Most of frameworks use this way for performance improvements, registering things, executing certain events, and making things simpler for developer...
For example if you want to create class "car" with is somewhere in
library
directory you would have to include the file first before you can create object of that class (miltiple lines of code, more room for error). The framework will create the class and includes related files in 1 line of code (easier and safer).Framework way also works as a factory. Instead of recreating an object, it will create object only once and every time you call the method again it will return the reference to existing object.
More things are happening behind the scenes when you use framework. Things are getting registered, etc...
CI 本身并没有取代类行为,它只是添加了功能,允许通过核心对象将自定义库/模型/视图作为单例对象进行访问,以实现简单性。
没有什么可以阻止您创建(就像我在我的一个项目中那样)带有非单例实体类的附加文件,并在模型中
需要
它们以供进一步使用。 事后看来,我可能应该为此使用帮助程序。加载器 (
$this->load
) 类的作用之一是创建单个对象指定类(模型、库或视图 - 但不是助手,见下文)并将其附加为通常可通过 $this 访问的核心类的属性。助手有点不同。它们没有附加,而是简单地从它们加载的位置“读”到全局命名空间中。
要回答您的问题,在不需要创建多个类实例的情况下,使用加载器类会更合适。如果您需要“实体”类,那么符合 CI 的最佳选择是将它们创建为助手。
CI doesn't replace class behavior per se, it simply adds functionality that allows access to custom libraries/models/views as singleton objects via the core object for simplicity.
Nothing is stopping you from creating (as I have in one of my projects) additional files with classes for non-singleton entities and
require
them in a model for further use. On hindsight, I should probably have used helpers for this.What the loader (
$this->load
) class does, among other things, is it creates a single object of the specified class (model, library or view - not helpers though, see below) and attaches it as a property of the core class that is normally accessible via $this.Helpers are a bit different. They are not attached, but instead simply 'read' into the global namespace from the point where they are loaded.
To answer your question, it would be more proper to use the loader class in instances where you don't need more than one instance of a class created. If you need 'entity' classes, your best CI-compliant bet would be to create them as helpers.
仅考虑到这种情况,这看起来像控制反转(也许我错了,我没有仔细研究 CodeIgniter)。
您不想像
new car()
中那样依赖car
类型。如果稍后您想让$var
成为racecar
该怎么办?$var
仍然可以做同样的事情,但它被迫成为一个car
因为你直接构造了它。或者,如果您正在测试此类,但car
是一些调用某些外部服务的复杂对象,该怎么办?您想测试您的逻辑,但不关心汽车服务是否不起作用。因此,您应该能够更改$var
以实际加载mockcar
。如果您执行$var = new car()
,则无法执行此操作。什么是控制反转?
Given only this context, this looks like Inversion of Control (maybe I'm wrong, I haven't looked too closely at CodeIgniter).
You don't want to rely on the type
car
as innew car()
. What if later you want to make$var
aracecar
?$var
can still do the same things, but it is forced to be acar
because you constructed it directly. Or what if you are testing this class, butcar
is some complex object which calls some external service. You want to test your logic, but don't care if the car service isn't working. So you should be able to change$var
to actually load amockcar
. You can't do that if you do$var = new car()
.What is Inversion of Control?