PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Jul 19, 2019 10:40 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Feb 15, 2012 4:38 pm 
Offline
Forum Contributor

Joined: Fri May 03, 2002 10:33 am
Posts: 108
Location: Cheshire
I confess to not having had a great deal of exposure to off-the-shelf frameworks, but in all that I have worked with I am struck by the seemingly inherent inefficiencies of the code.

Take CodeIgniter for instance. The MVC framework demands that my controller decides essentially what parameters to pass to one of the database models; gets the results back from there and hands them off to one of multiple views for rendering.

So, every time it does this, which is for literally everything, an array of data is produced by looping through database results, and then passed to the view which then loops through the array. I understand that MVC is a standard approach to code design, but despite (or perhaps because of) my lack of formal training in the area, I see that as twice as inefficient as it needs to be.

Now, I've written systems which take a different approach to MVC, but essentially (in my head anyway) follow the basic ethos in keeping display away from logic. Pretty much every PHP page which displays data from a database has four basic display routines to run for each possible query: no_results, before_results, result_row and after_results.

So, I've specified these as an interface, for example, and all my result set views are based upon it, and override or inherit those methods. Basic example

Syntax: [ Download ] [ Hide ]
<?php
class directory implements view {
        function no_results(){
                ?>
                <p>No results found</p>
                <?php
        }

        function before_results($count){
                echo '<p>'.$count.' results found.</p>';
                echo '<table>';
        }

        function result_row($row){
                echo '<tr><td>'.$row['name'].'</td><td>'.$row['number'].'</td></tr>';
        }

        function after_results($count){
                ?>
                </table>
                <?php
        }


}
 


Then, the controller works out what parameters to pass to the database model and passes the view to it.

Syntax: [ Download ] [ Hide ]
<?php
$view = new directory();
query::people($view, $var1, $var2, $var3);

class query {
        function people(view $view, $var1=false, $var2=false){
                .. do query stuff ..

                if(!$num_rows){
                        $view->no_results();
                }
                else {
                        $view->before_results($num_rows);
                        while($row = mysqli_fetch_assoc($qry)){
                                $view->result_row($row);
                        }
                        $view->after_results();
                }
        }
}
 


Obviously, there's stuff I'm missing out like pagination and query limits but that's the general gist. So, am I mental or is this a reasonable approach to take? I've not been exposed to anything similar. Is this more efficient. Opinions/evidence appreciated.

Cheers
Lee


Top
 Profile  
 
PostPosted: Wed Feb 15, 2012 6:26 pm 
Offline
Forum Newbie

Joined: Sun Feb 12, 2012 11:33 pm
Posts: 24
I think you need to read more about the MVC pattern... i don't think you fully understand how MVC works nor CodeIgniter IMO.

You are telling that the Controller decides what parameters to pass to the model, well that is wrong, when you create a Model you are the one who define what parameters needs to be passed to the model, and in the controller, when you call the model, you are the one who pass those parameters, not the controller by itself...

And then you are the one who decides what to do with the data returned from the model...

There are plenty of PHP Frameworks, for example, i use Laravel for small projects, but CodeIgniter is my number 1 for larger projects, it is very simple to use, easy to understand and to code on...

But if you want something more modular, code it from Scratch, build your own Framework.


Top
 Profile  
 
PostPosted: Thu Feb 16, 2012 11:34 am 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
Copying a small string/array from the model to the view is a small amount of memory consumed. I'd be more concerned about the amount of time/resources for the framework to get from processing the http request, to dispatching my controller action. In Zend Framework for example this can be over half a second, which is a problem for some projects.


Top
 Profile  
 
PostPosted: Sun Mar 11, 2012 11:39 pm 
Offline
Forum Newbie

Joined: Sat Mar 03, 2012 6:43 am
Posts: 19
I would agree with you that view layer must be separated from the controller layer. And more over that view layer has two branches the template branch and the view generator branch (that takes an object or a list of objects and produce a view segment) but … I couldn’t tell anybody that to say “hello ”.$name should use a template or a view generator class. It all has to do with what you are doing and how this affects the clearness of layers separation. Theory is great but when its apply is too strict and has nothing to do with real world applications be manipulated in practice, so theory has to take in consideration how things are done in order to be solid.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group