Friday, December 24, 2010

The Flexibility of PHP

I have been using PHP for 4 years now and I like to think I know it pretty well by now. It's a great language, it is open source, has regular updates and fixes, and has a great online manual.

What you also get when you talk to other developers who use other technologies is that it is non-complied and uses the 'Spaghetti' coding style where you have SQL in with your code, along with the formatting of the results returned from the database and all your HTML.

Well, for them and other people who are looking to learn a Server side Web Programming language, PHP is not sub standard, PHP is flexible - let me explain. People are also under the opinion that PHP is an easier language to learn than many other languages and yes it can be, but only because it is flexible and allows you to explore it from a number of different angles.

First, let's take a look at the 'Spaghetti' option, yes it is a little in efficient and yes it CAN be hard to edit if you have to come back to it in the future. But if you are new to programming and web development then it is the perfect way to start. You can learn the other (multi-tier) methods when you feel a little more comfortable. When you first learn to program, learning using a procedural manner is a great way to learn a language, the way it behaves and how logic determines specific conditions. Once you have these skills mastered, you can then start to apply them to more advanced methods, i.e. Object Oriented Programming.

For those who have never seen a bit of PHP 'Spaghetti' Code it looks a little like this:

<?php
    $SQL = "SELECT field1 FROM table";
    $query= mysqli_query($databaseLink,$SQL);
    while($row = mysqli_fetch_array($query,MYSQL_ASSOC)) {
       echo '<div class="tableResult">'.$row['field1'].'</div>';
   }
?>

The other option is to use a multi-tiered approach which could use Object Oriented Methods. When you use this approach, it works around the basis of separating code into different sections. When I first learnt OOP I used a two tier approach as I was a freelance developer and as such I thought that it was the best way to go. My two tier approach could very be different from another developer's 2 tier approach, but again, this is the flexibility of PHP. My 2 approach worked on the basis of keeping my Application Logic and my presentation in 1 tier and my business and data logic in another tier. When I created an application, I would follow this method and it served me well, across numerous (and large) applications. If I wanted to use a class, I would invoke it in the logic based file, the results would be sent back and the logic based file would process, format and output the results. The next example uses a separate database class but premise is the same, that the database and application classes from the controller or or input decision file.

//database class
class database {
public function retrieveResults ($SQL) {
$rows = array();
$query= mysqli_query($database,$SQL);
while($row = mysqli_fetch_array($query,MYSQL_ASSOC)) {
$rows[] = $row;
}
return $rows;
}
}

//application class
class application {
public function getTableData() {
$SQL = "SELECT field1 FROM table";
$useDatabaseClass = new database();
$results = $useDatabaseClass->retrieveResults($SQL);
return ($results);
}
}

//calling script
$useApplication = new application();
$getData = $useApplication->getTableData();

foreach ($getData as $row) {
echo '<div class="tableResult">'.$row['field1'].'</div>';
}

Then there is the 3 tier approach - one popular name for it is the MVC approach. This is where you separate application logic, data handling and presentation in to their three separate components. This is useful to use when you on any project, but I found it of most useful since I have been working in a design agency, as I work on multiple projects on any one time and front end guys who work there need to access many of the view files. Please note that the way you call scripts in reality would probably not look like the above, these are just for illustration purposes. In reality, if you use an MVC framework or you create your own, you would more likely extend your base classes rather invoking them using the new keyword.

The three tier is very similar to the two tier approach. Except that the $getData array is passed through to another file which outputs the results using the foreach loop. That's it, the hard work is really done when you move from one tier to two tiers, the third tier (the view tier) is really just a minor extension of the controller tier, which in my opinion works in tandem with the Model or Business Tier. The view or presentation file can be loaded in a number of ways, using include() or require() or even eval().

A few last words, when you start down your PHP developer career, do not let anyone tell you which method to use, weigh up the situation and make an informed decision. I like to build my online applications using 3 tiers using codeigniter as the MVC framework, but I still write single standalone scripts when I feel the situation needs it

Labels: , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home