php 在 CodeIgniter 中加载自定义类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4415240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading custom classes in CodeIgniter?
提问by Tarka
Just starting to use CodeIgniter, and I'd like to import some of my old classes for use in a new project. However, I don't want to modify them too much to fit into the CI way of doing things, and I'd like to be able to continue to use NetBeans' autocomplete functionality, which doesn't work too well with CI.
刚刚开始使用 CodeIgniter,我想导入一些旧类以用于新项目。但是,我不想对它们进行太多修改以适应 CI 的做事方式,而且我希望能够继续使用 NetBeans 的自动完成功能,该功能在 CI 中不太适用。
So, what is the best way to load custom classes & class files into CodeIgniter without having to use the library/model loading mechanisms?
那么,在无需使用库/模型加载机制的情况下将自定义类和类文件加载到 CodeIgniter 中的最佳方法是什么?
I apologise if this is something I should be able to find quickly, but I can't seem to find what I'm after. Everything I see is just telling me how to go through CI.
如果这是我应该能够快速找到的东西,我深表歉意,但我似乎无法找到我想要的东西。我所看到的一切只是告诉我如何通过 CI。
采纳答案by Roberto
I'd say you at least write a wrapper class that could require
the classes and instantiate the objects and make them accessible. Then you could probably autoload such library and use it as needed.
我会说你至少写了一个包装类,它可以require
将类和实例化对象并使它们可访问。然后你可能会自动加载这样的库并根据需要使用它。
I would recommend that you at least tried to have them fit in the CI way, as moving forward this will make you life much more easy. I've been in kind of the same position and learned just this along the way.
我建议您至少尝试让它们适合 CI 的方式,因为向前推进这将使您的生活更加轻松。我一直处于相同的位置,并在此过程中学到了这一点。
回答by Raza Ahmed
To do it codeigniter way, place your custom classes in libraries folder of codeigniter. And then use it by adding that class as library in your controller like this:
要以 codeigniter 方式进行操作,请将您的自定义类放在 codeigniter 的库文件夹中。然后通过在控制器中添加该类作为库来使用它,如下所示:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Someclass {
public function some_function()
{
}
}
/* End of file Someclass.php */
using in controller:
在控制器中使用:
$this->load->library('someclass');
checkout complete article at http://www.codeigniter.com/user_guide/general/creating_libraries.html
在http://www.codeigniter.com/user_guide/general/creating_libraries.html结帐完整的文章
回答by Phil Sturgeon
Libraries are easy to write but they have a few restrictions. Constructors can only take an array as a parameter and it's assumed that only one class will exist per file.
库很容易编写,但它们有一些限制。构造函数只能将数组作为参数,并且假定每个文件只存在一个类。
You can include any of your own classes to work with them however you want, as this is only PHP ofc :)
您可以根据需要包含任何自己的类来使用它们,因为这只是 PHP ofc :)
include APPPATH . 'classes/foo.php';
$foo = new Foo;
Or set up an __autoload() function in your config.php (best place for it to be) and you can have access to your classes without having to include them.
或者在你的 config.php(最好的地方)中设置一个 __autoload() 函数,你可以访问你的类而不必包含它们。
回答by YudhiWidyatama
If you're just starting to use CodeIgniter, maybe you ought to check Kohana (http://kohanaframework.org/). It is very similar to CodeIgniter in many ways but it loads classes in the normal way (using new ClassName()) so Netbeans' autocompletion features should works normally.
如果您刚刚开始使用 CodeIgniter,也许您应该查看 Kohana (http://kohanaframework.org/)。它在许多方面与 CodeIgniter 非常相似,但它以正常方式(使用 new ClassName())加载类,因此 Netbeans 的自动完成功能应该正常工作。