browser lang:en
Model view controller architecture has been a staple of desktop application development for a hell of a long time, with the increasing complexity of these newfangled web applications and websites in general the encapsulation and flexibility of mvc design makes darn good sense (buzzwords bolded for the executives out there). But what happens when a good design idea meets a really really evil language...
Well ok, php isn't evil per se, it's just misunderstood. Grossly, horrifyling misunderstood on a level that only economists and libertarians can appreciate (and maybe perl programmers but they enjoy that kind of thing). It's simple, php allows people to write really bad code and still accomplish whatever it is they want to do and fast. However, it is possible to write readable, maintainable code in php and using mvc will help with that. So without further ado here's my interpretation of mvc in php.
The model is the interface to the database. All database specific code goes in here. If you are using flat files, tables, and/or smoke signals do all the loading and saving in the models. Here's the model for the example.
File model: animal.mdl
class Animal
{
public static function getAnimal($animal_id)
{
$query = "SELECT * FROM animals WHERE animal_id='$animal_id'";
$result = mysql_query($query);
$row = mysql_fetch_object($result);
$animal = array();
$animal['animal_id'] = $row->animal_id;
$animal['color'] = $row->color;
return animal;
}
public static function updateAnimal($animal_id, $animal_color)
{
$query = "UPDATE animals SET color='$animal_color' WHERE animal_id='$animal_id'";
msyql_query($query);
}
}
include('Animal.mdl');
if ( isset($_REQUEST['animal_submit']) )
{
Animal::updateAnimal($_REQUEST['animal_id'], $_REQUEST['animal_color']);
}
$animal = Animal::getAnimal($_REQUEST['animal_id']);
include('Animal.tmpl');
<form action="Animal.php" method="post">
<input name="animal_id" value="<?php echo $animal[animal_id]; ?>" type="hidden">
<input name="animal_color" value="<?php echo $animal[color]; ?>" type="text">
<input name="animal_submit" value="Update" type="submit">
</form>
Have you ever had to develop something yourself only to find out that there had already been…
in:JQuery (0 comments)The situation begins with your blog or website and you need to post some code on a particular…
in:The Holy Faq's (0 comments)So. Google just recently announced Google Buzz. I’m not sure about you, but I…
in:Blog (0 comments)There's lots of clever scripts around to tell you how to get images in and out of…
in:PHP scripts (0 comments)Spherical Law of Cosines Suppose that we want to find the five nearest places to (47.470779, -87.890699) using Spherical…
in:MySql (0 comments)I have always used Dreamweaver, and love it. But, I have been thinking, what are the FREE CSS…
in:Free Software (0 comments)Advanced Linux Sound Architecture (known by the acronym ALSA) is a Linux kernel component intended to replace the…
in:Blog (0 comments)The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a…
in:Scripts and tutorials (0 comments)
