CodeIgniter Forums
Passing instance objects between controller and model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Passing instance objects between controller and model (/showthread.php?tid=64678)



Passing instance objects between controller and model - Shawn - 03-20-2016

Are there any gotchas to creating standard PHP classes and passing instances of them between controllers and models? I don't mind putting include_once(APPPATH.'models/Some_Php_Class.php'); at the top of my controller if it means I can group posted items into a single self-documenting object that has it's own validation methods, etc. that I can pass to the model. Or equally, I would like to pull a collection of object instances into the controller and build selection arrays based on a criteria.

Could I run into concurrency issues with this practice?


RE: Passing instance objects between controller and model - Wouter60 - 03-20-2016

Imho CodeIgniter uses libraries to create objects that can be used by multiple controllers and models.
http://www.codeigniter.com/userguide3/general/creating_libraries.html


RE: Passing instance objects between controller and model - cartalot - 03-20-2016

+1 for Libraries, they do everything you are talking about.


RE: Passing instance objects between controller and model - kilishan - 03-20-2016

(03-20-2016, 09:20 AM)Shawn Wrote: Are there any gotchas to creating standard PHP classes and passing instances of them between controllers and models? I don't mind putting include_once(APPPATH.'models/Some_Php_Class.php');  at the top of my controller if it means I can group posted items into a single self-documenting object that has it's own validation methods, etc. that I can pass to the model. Or equally, I would like to pull a collection of object instances into the controller and build selection arrays based on a criteria.

Could I run into concurrency issues with this practice?

No issues that I'm aware of. My current practice is similar, using standard PHP classes and I have the model create an instance of that class and fill in the class properties based on table column names. Then the model has a save method which determines if it's an update/insert based on whether it's primary key is null or not.

So, nope. Go for it.


RE: Passing instance objects between controller and model - sintakonte - 03-23-2016

in order to prevent include_once calls we made a pre controller Hook which autoloads those classes 
and all classes are stored within a new folder called objects inside the application directory

PHP Code:
class AppAutoLoadObjects {

    private 
$arrObjectPaths = array(
        
'objects/',
        
'objects/core/'
    
);
            
    public function 
initialize()
    {
        
spl_autoload_register(array($this,'autoloadObjects'));
    }

    public function 
autoloadObjects($class)
    {
        if(
strpos($class'CI_') !== 0) {
            foreach(
$this->arrObjectPaths as $dir) {
                if (
file_exists(APPPATH.$dir.'/'.$class.'.php'))
                {
                    require_once(
APPPATH.$dir.'/'.$class.'.php');
                }
            }
        }
    }


and the hook in your hooks.php is very simple

PHP Code:
$hook['pre_system'][] = array(
    
'class' => 'AppAutoLoadObjects',
    
'function' => 'initialize',
    
'filename' => 'AppAutoLoadObjects.php',
    
'filepath' => 'hooks'
);