[eluser]bcdennis[/eluser]
After developing a little while in CI, I found myself wanting to develop a class hierarchy that violates the MVC architecture (as I understand it) implemented in CI. For instance, I have a calendaring type application going and I wanted a base class (called 'Schedulable') that I wanted all schedulable events to inherit from. But how do you do this as a CI model? Or a CI library? I know you could create a MY_Model that handles the basic scheduling routines for others to inherit from, but that didn't feel right to me. So what I did was as follows:
1.) Created a two new folders in the application folder; 'application/factories' and 'application/classes'.
2.) Created a new factory model who's responsibility is to instantiate object factories and assign any CI goodies needed to that factory. (such as $CI->db).
3.) Roll whatever classes I need into the 'application/classes' folder and create a matching factory in the 'application/factories' folder.
Snippet from 'models/factory_model.php'
Code:
class Factory_model extends model
{
/**
* Singleton for the Reservation Factory.
*
* @access public
* @param none
* @returns object The Reservation Factory object.
*/
public function reservation_factory()
{
if($this->reservation_factory == null)
{
require_once(APPPATH.'factories/reservation_factory'.EXT);
$this->reservation_factory = new Reservation_factory();
$this->reservation_factory->db =& $this->db;
}
return $this->reservation_factory;
}
Snippet from 'factories/reservation_factory.php'
Code:
class Reservation_factory
{
/**
* Reservation Factory constructor.
*
* @access public
* @param none
* @return none
*/
public function __construct()
{
require_once(APPPATH.'classes/base_class'.EXT);
require_once(APPPATH.'classes/schedulable_class'.EXT);
require_once(APPPATH.'classes/reservation_class'.EXT);
log_message('debug', 'Reservation Factory Class Initialized');
}
And then adding whatever factory methods I needed to the reservation_factory.
What I found is that this gives me another layer of abstraction with which to build my application. I can encapsulate logic specific to schedulable events in the schedulable class, while implementing a higher level of business logic in a calendar_model. So far, I'm getting smaller, more cohesive classes & models, which (to steal a phrase from Martin Fowler) just "smell's right".
Hope this helps anyone out there.