Loading custom classes in CodeIgniter?

前端 未结 4 1552
醉话见心
醉话见心 2021-01-02 00:49

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

相关标签:
4条回答
  • 2021-01-02 01:23

    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:

    <?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

    0 讨论(0)
  • 2021-01-02 01:23

    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.

    0 讨论(0)
  • 2021-01-02 01:30

    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 :)

    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.

    0 讨论(0)
  • 2021-01-02 01:31

    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.

    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.

    0 讨论(0)
提交回复
热议问题