![]() |
model's constructor with parameters - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: model's constructor with parameters (/showthread.php?tid=61261) Pages:
1
2
|
model's constructor with parameters - casa - 04-05-2015 Hello, I want to create a model which extends CI_MODEL and in my constructor, iwant to pass some parameters. Exemple : class Test extends CI_Model { private $items ; public function __construct( $params) { parent::__construct(); $this->items = $params ; } public function getItems() { return $this->items ; } } When i load my model in my controler or everywhere else i do this : $this->load->model('Test'); $r = new Test('toto') ; i have this message : "Missing argument 1 for Test::__construct()". i had try this : class Test extends CI_Model { private $items ; public function __construct( $params = '') { parent::__construct(); $this->items = $params ; } public function getItems() { return $this->items ; } } and then when i do it : $this->load->model('Test'); $r = new Test('toto') ; echo $r->getItems () ; // 'toto' No problem ![]() Is my method is the right way to practice with CodeIgniter ? Thanks for your answers ![]() RE: model's constructor with parameters - gadelat - 04-05-2015 You are not supposed to use it this way. It reports error because $this->load->model automatically instantiates the model for you into $this->test, you are not supposed to do it yourself. Leave the constructor alone and put that logic into separate method (or access property directly). RE: model's constructor with parameters - casa - 04-05-2015 (04-05-2015, 03:23 PM)gadelat Wrote: You are not supposed to use it this way. It reports error because $this->load->model automatically instantiates the model for you into $this->test, you are not supposed to do it yourself. Leave the constructor alone and put that logic into separate method (or access property directly). OK. but explain why exactly ? and why is not good ? What is the problem with this method ? I inherit a class and I can normally redefine all its methods if necessary. How i do to make constructors with parameters ? Give an example. I teach in engineering school and say no and not explain why, is not educational. In POO, it's important to have the possibility to make constructors with parameters. RE: model's constructor with parameters - gadelat - 04-05-2015 I did explain why. $this->load->model included AND instantiated your model, that's why it gives you error. If you don't want it to do that, don't use $this->load->model RE: model's constructor with parameters - s4if - 04-06-2015 (04-05-2015, 09:01 PM)casa Wrote:(04-05-2015, 03:23 PM)gadelat Wrote: You are not supposed to use it this way. It reports error because $this->load->model automatically instantiates the model for you into $this->test, you are not supposed to do it yourself. Leave the constructor alone and put that logic into separate method (or access property directly). it is because if you use $this->load->model, codeigniter will instantiate your model automatically "Without" giving any "Parameter" to it. so, it gives you errors.. if you use PDO in Codeigniter, you can call it with $this->db->[your function], because codeigniter wrap PDO in it's database class.. [CMIIW] RE: model's constructor with parameters - casa - 04-06-2015 thanks. i suppose an example like this : PHP Code: // ______________________ RE: model's constructor with parameters - Athov - 04-06-2015 why not do PHP Code: class Test extends CI_Model and then PHP Code: $this->load->model('test'); as for db items PHP Code: class Test extends CI_Model RE: model's constructor with parameters - casa - 04-06-2015 Note that your method launch an error when i test it :"Undefined property: CI_Loader:Test" and " Call to a member function setItems() on a non-object" PHP Code: $this->load->model('test'); Then, if i want to complete an array of object Test and manipulate datas of many ways, i need to do that (for example): PHP Code: $this->load->model('Test'); RE: model's constructor with parameters - Athov - 04-06-2015 (04-06-2015, 02:23 AM)casa Wrote: Note that your method launch an error when i test it :"Undefined property: CI_Loader:Test" and " Call to a member function setItems() on a non-object" of course it will lunch an error I used your model name but a CI model needs "_model" in his name like class Test_model extends CI_Model{} file name: Test_model.php and when you loaded $this->load->model('test'); you will access it by $this->test_model and if you want to name it something else use $this->load->model('test', 'new_model_name'); and you will access it by $this->new_model_name and you dont need to use $r=new Test(); $this->test_model has it by default best example: http://www.codeigniter.com/userguide3/general/models.html RE: model's constructor with parameters - casa - 04-06-2015 No need "_model" in the name. When you create a model, you name the php file like the name model. The loader.php of the core search the file as the model name. Example : test.php and inside, you create class Test extends CI_MODEL PHP Code: class Test extends CI_MODEL PHP Code: $this->load->model('Test') ; PHP Code: $this->load->model('Test') ; PHP Code: // example With your method you can't do what i describe with function setItems. That never works. |