Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So, what is the best way to load custom classes & class files into CodeIgniter without having to use the library/model loading mechanisms?

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.

like image 394
Tarka Avatar asked Dec 11 '10 04:12

Tarka


3 Answers

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.

like image 171
Roberto Avatar answered Nov 14 '22 12:11

Roberto


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

like image 43
Raza Ahmed Avatar answered Nov 14 '22 14:11

Raza Ahmed


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.

like image 16
Phil Sturgeon Avatar answered Nov 14 '22 14:11

Phil Sturgeon