I'm bothered by framework inefficiency. Should I be?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

I'm bothered by framework inefficiency. Should I be?

Post by leenoble_uk »

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

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
	}


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

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();
		}
	}
} 
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
G l a z z
Forum Newbie
Posts: 24
Joined: Sun Feb 12, 2012 10:33 pm

Re: I'm bothered by framework inefficiency. Should I be?

Post by G l a z z »

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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: I'm bothered by framework inefficiency. Should I be?

Post by josh »

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.
kon
Forum Newbie
Posts: 19
Joined: Sat Mar 03, 2012 5:43 am

Re: I'm bothered by framework inefficiency. Should I be?

Post by kon »

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.
Post Reply