In this article I am going to explain one of possible ways of developing PHP applications in a team. This method emerged during our work on information system for students of the Ankara University (http://ogrenci.ankara.edu.tr).
I would like to thank Onur Boyaci and Dincer Akay and all the other people who took part in the project.
I would call it a HTML encapsulation method because rather that embed PHP in HTML, HTML is encapsulated and 'echo'ed by PHP:
...
<table>
<?php
for ( $i=0; $i<10; $i++ )
echo "<tr><td>This is line number $i</td></tr>";
?>
</table>
...
Another aspect is that different PHP files are used. For example for search page we would have a 'search.php' and for login page 'login.php'.
I would call this method a 'CGI' method because it resembles traditional CGI programming approach. Such a wide use of it is probably influenced by that CGI tradition, general 'programming' approach and some features of HTML, namely luck of aims for separation content from presentation.
This model has many good points. It offers a better control over execution and presentation of the content, aids separation to functions by enabling a programmer to write chunks of HTML code in functions thus helping to structure the program up; which in turn makes it more understandable and manageable. Giving more control to programmer helps to manage resources in a more rational fusion.
But there are shortcomings. HTML encapsulated that way is very hard to update with WYSIWYG tools, there is a lot of recurrence and code becomes cluttered.
Let me first outline the concept and then explain it in detail:
Idea is to collect all the common functionality in the same place. I'm using the following structure:
<?php
// Include all the necessary files
include("db.abstraction.php");
...
// Connect to the database
sqlConnect();
...
// Perform the necessary action
switch( $action )
{
case "add":
addNote( $title, $body );
break;
...
}
...
// Select the appropriate page to show
switch( $page )
{
case "update":
include("update.php");
break;
...
}
...
// Disconnect from the database
sqlDisconnect();
?>
So what are the advantages of this approach?
And what are the disadvantages? Hmm, well, resource management could be better - database connections are opened even for files which do not access database in any way and you have to pass extra parameters in your queries. 'action' and 'page' in this case.
Writing database-related functions I've noticed a very interesting thing: I've only done 3 different things:
This can be summed up in 3 database abstraction layer functions plus connection and disconnection:
sqlConnect()sqlExec( $query )sqlGetResult( $query )sqlGetRow( $query ) sqlDisconnect()Once you want to switch to another database or just change something in the way it works, just change the abstraction layer functions! So much for the abstraction layer.
As opposed to encapsulating HTML. Idea is to leave as much as possible of HTML intact so that people working on HTML design can easily redesign the page if necessary. Let's rewrite the first example conforming to this method:
...
<table>
<?php
for ( $i=0; $i<10; $i++ )
{
?>
<tr><td>This is line number <?php echo $i; ?></td></tr>
<?php
}
?>
</table>
...
Hope you see the point.
Another point is to separate files by functionality so that all the project becomes more manageable and easier to write as a team. In the example following this chapter, files separated as following:
To illustrate the ideas I explained above I have written a small notebook using MySQL database. You can download it from here: notebook.tar.gz.
Now let me show how this project can be developed by a team. Usual development cycle consists of:
After the last stage usually a second cycle follows. I will skip testing and deployment here.
Let's start from the analysis. We want a user of our notebook to be able to:
And a note should consist of:
Which means that we will have to design following pages:
and following database utility functions:
addNote( $title, $body ) - to add a new noteupdateNote( $id, $title, $body ) - to update an existing oneremoveNote( $id ) - to remove a notelistNotes() - to list all the notes (titles)getNote( $id ) - to get a complete note (title and body)Now it's time for team work. Different people can be assigned to the following fields:
As for the abstraction layer, once written it can be just copied from project to project with minor changes. With abstraction layer in place, writing database utility functions becomes all too trivial:
<?php
...
function addNote( $title, $body )
{
execSQL( "insert into notes (title,body) values ('$title','$body')"
);
}
...
?>
Refer to the code for detail on writing other files.
Hope the method I have described above would be helpful as it have been for me. And sorry for poor english and style.