I'm bothered by framework inefficiency. Should I be?
Posted: Wed Feb 15, 2012 3:38 pm
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
Then, the controller works out what parameters to pass to the database model and passes the view to it.
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
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
Code: Select all
<?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
}
}
Code: Select all
<?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();
}
}
}
Cheers
Lee