Welcome Guest, Not a member yet? Register   Sign In
Want a little OO in your MVC?
#1

[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.
#2

[eluser]ch5i[/eluser]
This sounds very interesting...

I've got a procedural background, so if you don't mind:

Could you elaborate on how you use this in your higher level models?

And does this
Code:
require_once(APPPATH.'classes/base_class'.EXT);
require_once(APPPATH.'classes/schedulable_class'.EXT);
require_once(APPPATH.'classes/reservation_class'.EXT);

mean that you extend the classes like : base_class <- schedulable <- reservation?
#3

[eluser]adamp1[/eluser]
Not too sure what your on about here, I do something similar with my applications.

I have a general Model class called 'Base_Model' and it provides more complex insert/update etc code than the Active record class does. I just make this model load by default in the autoloader and then extend it with other models.

But I do also do something very similar with my Controller classes. I want to use another layer between my Controllers and the CodeIgniter Controller class. So I define this in a MY_Controller.php file. I don't get why you don't do this? It then simplifies loading and also means you can use CodeIgniter calls inside the classes, you don't need all that require code and separate folders.
#4

[eluser]ch5i[/eluser]
I think with bcdennis' approach it is easier to give your give your classes modular functionality, i.e. add 'schedulable' to any class you want. - That's how I understood it Smile
#5

[eluser]adamp1[/eluser]
How its any easier? I just create a new class and extend my base class? You can't get any easier than that.
#6

[eluser]Rick Jolly[/eluser]
Maybe I'm misunderstanding, but looks like bcdennis has created a factory class that simply includes classes. Essentially he/she has done what __autoload() does with a lot more work. He/she is using php5 (based on __construct) and class naming conventions (the classes end with "_class") which are the only 2 ingredients needed for __autoload().

Just register an autoload function.




Theme © iAndrew 2016 - Forum software by © MyBB