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/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: To use associative arrays violates the principles of OOP? (/showthread.php?tid=29916)



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-27-2010

[eluser]Caio Russo[/eluser]
hi Giga,

think that way:

if you have a variable called $p_iva_f and change it into $p_iva_fornitore you have to change it in any levels, right?

so, if you have a array with some index name and change that index name, it's not the same?

Cheers.

Caio


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

[eluser]Giga[/eluser]
[quote author="Caio Russo" date="1272442392"]hi Giga,

think that way:

if you have a variable called $p_iva_f and change it into $p_iva_fornitore you have to change it in any levels, right?

so, if you have a array with some index name and change that index name, it's not the same?

Cheers.

Caio[/quote]
Thanks for the reply, but if I use a class:
Code:
class Fornitorem {
    
        var $p_iva;
        var $nome;
        var $indirizzo;
        var $tel;
        var $email;
        
        function setFornitorem($iva,$nomef,$ind,$telf,$emailf)
        {
            $this->p_iva=$iva;
            $this->nome=$nomef;
             $this->indirizzo=$ind;
            $this->tel=$telf;
            $this->email=$emailf;
        }
        function _construct()
        {
            $this->p_iva="";
             $this->nome="";
             $this->indirizzo="";
             $this->tel="";
             $this->email="";
        }
    
        function setPiva($iva)
        {
            $this->p_iva=$iva;
        }
.......
}
Now in my controller call a method of fornitore_model passing an object of class above
Code:
$this->load->library('fornitorem');
$this->load->model('fornitore_model','',TRUE);
$this->fornitorem->setFornitorem($_POST['p_iva_f'],$_POST['nome'],$_POST['indirizzo'],$_POST['tel'],$_POST['email']);
$this->fornitore_model->aggiungiFornitore($this->fornitorem);
This does not depend on the field of array


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

[eluser]Caio Russo[/eluser]
Hi Giga.

Absolutely right, but it's a different approach from what you asked before.

If u have a function that receive parameters, doesn't matter if you change the name. But if you use the array or variable directly then you have to change.

Caio


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

[eluser]Giga[/eluser]
[quote author="Caio Russo" date="1272482956"]Hi Giga.

Absolutely right, but it's a different approach from what you asked before.

If u have a function that receive parameters, doesn't matter if you change the name. But if you use the array or variable directly then you have to change.

Caio[/quote]
My question is:
What is the best approach?
In my opinion the latter approach to observe the principles of software engineering as reuse, maintainability, low dependence.

You think so or am I wrong?

PS: I have talked about this concept because I have to develop a project of software engineering. So this is not only to implement a workable solution but justify what you are doing in the documentation of the project, adopting the "best" solution.

Thanks for your patience Smile


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

[eluser]Caio Russo[/eluser]
Hi Giga,

Check this page OOP with PHP that may answer your questions.

Cheers

Caio


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

[eluser]danmontgomery[/eluser]
This is a logical fallacy... A model is a class, there's no reason you can't accomplish what you have in your second example with a model, simply by making the class a model. You have indeed added a layer of abstraction, but you've also changed the logic of the code, which is the real benefit of the abstracted approach, not that it's done outside of the model.


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

[eluser]Caio Russo[/eluser]
hi Giga,

Again it's 2 different things. MVC is an architectural pattern. Doesn't means that it "must" be or use OOp at all.

It could simply be developed with a procedural programing language and still use MVC patterns.

cheers

Caio