14

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.

5 Answers 5

24

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

Sign up to request clarification or add additional context in comments.

Comments

17

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.

1 Comment

including you files in config is a bad call
11

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.

1 Comment

Since it's just 3 classes for abstracting DB & Memcache access, I took the approach I wound up taking. It wasn't really a technical issue, more of a design issue.
0
require_once(PHYSICAL_BASE_URL . 'system/application/controllers/abc.php');
$report= new abc();

Next use the function detail in abc contoller:

$mark=$report->detail($user);

Comments

-4

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.

1 Comment

Only 2.x has any similarities to CI, the 3.x branch is totally different. They only really share the fact that they use PHP and MVC...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.