CodeIgniter Forums
To use associative arrays violates the principles of OOP? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: To use associative arrays violates the principles of OOP? (/thread-29922.html)



To use associative arrays violates the principles of OOP? - El Forum - 04-26-2010

[eluser]Giga[/eluser]
Hello everyone,
I am an university student who needs to develop a web application for a project in software engineering.

Having to respect the principles of OOP, I asked myself some questions:
Using MVC should be no independence between the levels ... but in many video tutorials (youtube) known as the use of associative arrays.
Let me explain:
Code:
class ControllerX extends controller
{....
function inserisciX()
{
$options['p_iva_f']=$_POST['p_iva_f'];                
$options['nome']= $_POST['nome'];
$options['indirizzo']=$_POST['indirizzo'];                        

$this->load->model('fornitore_model','',TRUE);
$id = $this->fornitore_model->aggi
ungi($options);
}
}            

class ModelX extends model
{....
function aggiungi($options=array())
{
      $this->load->database();
      $fornitore = array(  'p_iva_f'=>$options['p_iva_f'],'nome'=>$options['nome'],
                        indirizzo'=>$options['indirizzo']);
            
             $this->db->insert('fornitore',$fornitore);
             return $this->db->insert_id();
        }
}
Now if I change in the controller:
$options[‘p_iva_f’] in $options[‘p_iva_fornitore’]
I have to change even in the Model!
The independence between the layers is lost in this way ???!!!

So,I decided to create a class ( in library):
Code:
class Fornitore
{
... attributes
....methods
}
Now step away from control to model an object of that class! In your opinion is as good solution for a more OO? Or you have other solutions?

Thanks in advance


To use associative arrays violates the principles of OOP? - El Forum - 04-26-2010

[eluser]mddd[/eluser]
At some point, you will always need to know what your properties are called. You can make a data model (as you are doing, Fornitore class) but in this class you will still need to have properties like Fornitore->name, Fornitore->kind etc. So, if you want to call this from you controller you might say Fornitore->setName() in stead of saying $options['name'] = 'x'; but still, if you change Fornitore->name to Fornitore->Product_name, this will impact your Controller. Maybe a little bit less because when you change the property Fornitore->name, the method Fornitore->setName could still be called the same.

So, your controllers and models will have to know about each other somehow. It may be through an array or an object or whatever. In the end there will be some things you can't change without changing the other. And that's okay.

It's much better to use an array of options (as you did) then the "old" way of calling a function like add_furniture($name, $kind, $....) because that is really hard to change!