CodeIgniter Forums
Passing parameters to model constructor - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: Passing parameters to model constructor (/showthread.php?tid=68746)



Passing parameters to model constructor - CINewb - 08-19-2017

Sorry, I know this has been covered before.  Some of the questions regarding this are quite old, and I wondered why this functionality hasn't been made available as standard?

It would be really useful to be able to pass arguments to a model's constructor, along the lines of:

PHP Code:
$this->load->model('some_model'$args); 

Then in the model have:

PHP Code:
public function __construct($args) {


I know I could make my own initialization method and do:

PHP Code:
$this->load->model('some_model');
$this->some_model->init($args); 

However I have some arguments that are required for the whole model to work, and if I forget to call init() things will get somewhat messy.


RE: Passing parameters to model constructor - skunkbad - 08-19-2017

(08-19-2017, 03:08 AM)CINewb Wrote: Sorry, I know this has been covered before.  Some of the questions regarding this are quite old, and I wondered why this functionality hasn't been made available as standard?

It would be really useful to be able to pass arguments to a model's constructor, along the lines of:

PHP Code:
$this->load->model('some_model'$args); 

Then in the model have:

PHP Code:
public function __construct($args) {


I know I could make my own initialization method and do:

PHP Code:
$this->load->model('some_model');
$this->some_model->init($args); 

However I have some arguments that are required for the whole model to work, and if I forget to call init() things will get somewhat messy.

So force each method to check if the arguments have been initialized:

PHP Code:
if( $this->initialized // all is well