Model best practices - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Model best practices (/showthread.php?tid=43454) Pages:
1
2
|
Model best practices - El Forum - 07-11-2011 [eluser]sqwk[/eluser] Code: // Just passing the whole lot to the create function Is there any point/advantage in using a setter functions in a model instead of passing data directly to a create/update function? Code: // Setting data though predefined setter functions for protected vars. Model best practices - El Forum - 07-11-2011 [eluser]CodeIgniteMe[/eluser] You benefit from encapsulation and polymorphism. You can do this if you prefer, but use a property instead of using $this as an array. Code: foreach($this->variables as $key=>$value) Model best practices - El Forum - 07-12-2011 [eluser]sqwk[/eluser] So for the model you would put all the variables in one 'super-array'? Or am I misunderstanding something? Model best practices - El Forum - 07-12-2011 [eluser]CodeIgniteMe[/eluser] Yes, you can use a 'property' of the model to stand as a container for all your arrays. And one more note, always clean your variable after you are done with it. Code: foreach($this->variables as $key=>$value) Model best practices - El Forum - 07-12-2011 [eluser]sqwk[/eluser] So why should I use a parent-property instead of looping over $this directly? Security? Faster? Model best practices - El Forum - 07-12-2011 [eluser]CodeIgniteMe[/eluser] Quote:So why should I use a parent-property instead of looping over $this directly? Security? Faster? Because $this is an object, and not an array, if you make $this as an array, the object instance will be overwritten. And also, using a 'parent-property' is the proper way to store values, etc. Model best practices - El Forum - 07-12-2011 [eluser]CodeIgniteMe[/eluser] And I just want to know, how would you implement to use $this as an array in the model class? Model best practices - El Forum - 07-12-2011 [eluser]sqwk[/eluser] The array is not the problem. foreach handles objects as well. Nothing is getting overwritten, it is just looping though. Ignore the explicit setters… simplified example. Code: class A_Model extends CI_Model { Model best practices - El Forum - 07-12-2011 [eluser]CodeIgniteMe[/eluser] Oh, I'm sorry, I was thinking you meant it like this Quote: Model best practices - El Forum - 07-12-2011 [eluser]CodeIgniteMe[/eluser] And may I ask, what would be the output of $var here: Code: public function create() { |