CodeIgniter Forums

Full Version: CI Way of Using Own Classes / Code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. After searching, reading the CI docs, looking at several online CI project examples and a number of github projects I can't figure this out.

What is the CI best practice to use for our own 'independent' classes and code in relation to CI's structure?

I can only find relevant info from examples in "Creating Libraries" I see 
From the doc topic "Creating Libraries" I read, "However, since a library is a class, ..."

I'm used to thinking of libraries as external code that comes in some "name.lib" format or, with PHP, like these:

I don't think of my own code as a library. I like bifurcating using  'Separation of Concerns.' Yet, I do not see any example of anyone 'new'ing' classes like in the code snippet below. (Note my question is general, not just about models.)

I've tried the (equivalent example ) code below in a working program; it works. Is it a bad practice to do that?

defined('BASEPATH') OR exit('No direct script access allowed');

require_once( 'src/BusinessLogic.php' );

class Bigshot_model extends CI_Model
    public function __construct()
    public function getManagersSalary()
        $bi = new BusinessLogic;
        $waste = $bi->calcBloatedEgoCost( $params );

I think I understand to get 'native' access to CI functions, et cetera, I may have to make a class a library class. Is it bad practice to pass a reference to $CI into a non-CI derived class?

Thank you.
What you are doing above is a good start. You have to reference the full path name to any class that you want to instantiate with new().

If you use composer then you can load your classes or APIs in this way:

use PhpOffice\PhpWord;
use PhpOffice\Common;
Lonnie Ezell's book gives an overview of using composer in codeigniter:

Codeigniter classes are singletons and so are any classes that inherit from them.

Passing in a reference to the $CI class is not a bad practice but it does couple your classes to the CI framework.
If your class has to reference any of the CI framework services then you need to reference it in some way.
Good advice and info above. Didn't know that CI classes were singletons, I like singletons, so cool.

Also good to know I'm not being 'evil' using CI like a normal MVC framework.

Thank you for the response, I appreciate it.
Hi, we have same belief. for me library is just like an external helper and using it to act like a controller is a very bad practice for me. I have restructured CI in my way for building a longterm project. Please take a look and discuss with me at my post if you aggree or disaggree with my coding structure.
(02-17-2017, 10:41 AM)JustBoo Wrote: [ -> ]I'm used to thinking of libraries as external code that comes in some "name.lib" format or, with PHP, like these:

I only use these PHP libraries when I have to complete a project or a module urgently for a client. I mean why reinvent the wheel? It is a bad thing or a bad practice? I don't think so. I have seen many CI websites that are using tons of libraries, for example Swiftmailer for emailing or PHPUnit for unit testing.