Welcome Guest, Not a member yet? Register   Sign In
Object-Oriented Design Patterns in CodeIgniter
#1

[eluser]bluecarrot16[/eluser]
Hello –

I'm no stranger to web development (started out with ASP.NET, recently moved to PHP), but I've just discovered CodeIgniter, and I'm really impressed. Reading through the documentation, everything just looked like it made sense (ie: I could see how I would use this). However, as I've started to implement my application, I've encountered some difficulties understanding how CodeIgniter deals with non-singleton classes.

For example, say I was building a portal/CMS application. Each page in the CMS could have some number of "modules." There could be, for instance, a base class "Module" which different types of modules (ie: Blogs, Forums, Polls, etc.) could extend. These extended classes would inherit or extend functions from the base Module class (methods for viewing and editing the module, methods for calculating permissions on the module, etc.) Then, in the model and/or controller, instances of these subclasses could represent the different modules on a given page.

Now, I understand how I could write the Module class, and the subclasses, then get those classes to utilize CodeIgniter (ie: using the getInstance method to grab a CodeIgniter object, etc.) What I'm struggling with is how exactly the Module class and its subclasses (which could each be instantiated multiple times) could be loaded into a CI model or controller. CI has all kinds of rich utilities for loading singleton classes (Models, Controllers, Libraries, each are "classes," but each of these classes is intended to be instantiated only once and to be used for the duration of the request). However, there doesn't really seem to be a way to just load a plain-old-non-singleton class.

Could somebody explain the "CodeIgniter way" of doing this?

Thanks so much.
#2

[eluser]xwero[/eluser]
The php way is to include the file and use following syntax
Code:
$instance1 = new Module_class();
$instance2 = new Module_class();

You can do this with all classes. To prevent initializing but making sure the class file is only included once you can load your class as a helper or plugin.
#3

[eluser]bluecarrot16[/eluser]
Alright. Yeah, I knew how to instantiate the class in PHP, I was just confused about the right way to load it in CodeIgniter. The "Helper or Plugin" part is what I was looking for. Considering the amount of hoop-jumping CI does in the Loader library (looked through the source the other day, just for kicks), I thought there must be a better answer than require_once()-ing in the middle of your controller somewhere. This will make the class globally available, yes?
#4

[eluser]xwero[/eluser]
I'm sorry i gave too much information, i didn't know how good your php knowledge was so i added it as pretext.

It will make the class globally available but you can't call it like the other CI classes. Loading a class as a helper is similar to include_once but the file is remembered on framework level.
#5

[eluser]bluecarrot16[/eluser]
Haha, no problem. Thanks so much.
#6

[eluser]nikefido[/eluser]
[quote author="bluecarrot16" date="1230216565"]Alright. Yeah, I knew how to instantiate the class in PHP, I was just confused about the right way to load it in CodeIgniter. The "Helper or Plugin" part is what I was looking for. Considering the amount of hoop-jumping CI does in the Loader library (looked through the source the other day, just for kicks), I thought there must be a better answer than require_once()-ing in the middle of your controller somewhere. This will make the class globally available, yes?[/quote]

A way around the "hoop jumping" is to use PHP 5's __autoload

However you need to implement that carefully (most likely within a factory class, or in CI's case, overriding some functionality in the loader class)




Theme © iAndrew 2016 - Forum software by © MyBB