简体   繁体   English

在CodeIgniter中加载自定义类?

[英]Loading custom classes in CodeIgniter?

Just starting to use CodeIgniter, and I'd like to import some of my old classes for use in a new project. 刚开始使用CodeIgniter,我想导入一些旧的类用于新项目。 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. 但是,我不想过多地修改它们以适应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。

To do it codeigniter way, place your custom classes in libraries folder of codeigniter. 要以codeigniter方式执行此操作,请将自定义类放在codeigniter的libraries文件夹中。 And then use it by adding that class as library in your controller like this: 然后通过在控制器中添加该类作为库来使用它,如下所示:

<?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 checkout完整文章, 网址http://www.codeigniter.com/user_guide/general/creating_libraries.html

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()函数(最好的位置),你可以访问你的类,而不必包含它们。

I'd say you at least write a wrapper class that could require the classes and instantiate the objects and make them accessible. 我要说你至少写一个包装类,它可以require类并实例化对象并使它们可访问。 Then you could probably autoload such library and use it as needed. 然后你可以自动加载这样的库并根据需要使用它。

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. 我建议您至少尝试让它们适合CI方式,因为向前推进这将使您的生活更加轻松。 I've been in kind of the same position and learned just this along the way. 我一直处于相同的位置并且一路上学到了这一点。

If you're just starting to use CodeIgniter, maybe you ought to check Kohana (http://kohanaframework.org/). 如果你刚刚开始使用CodeIgniter,也许你应该检查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非常相似,但它以正常方式加载类(使用新的ClassName()),因此Netbeans的自动完成功能应该正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM