PHP Project Example: Basic Structure, Source Code

The first part of a Series about php as well as sql, html and css in which the development process of a website is outlined.
The (quite imperfect) result may be seen here: Trigger-DB. The theme of the site is not really where my focus was, but just to get it out of the way: It is a website that stores information about (trauma-)triggers in movies, books, etc.


I decided to take a closer look at php – and contiguously html, sql and – to a certain extend – css. My motivation for this was that I wanted to write some web-based application/website (nothing specific, just in general) and my only experience in this up to now was a Java EE project (which was fun, but I do not think it will be the future of web-bases applications/websites – not saying php will be…).

My existing knowledge: As I already took a database class in college I am academically familiar with sql and I have read some php as well as html code though not written any prior to now.

General Structure of php project

I decided to loosely follow the model-view-controller pattern in which the model holds and persists the data, the view displays it and the controller manages what the view displays and executes operations on the model.

The separation of controller and view is not executed as well as it should be, especially in case of forms. As a consequence the controller is a bit bloated (and to be honest, not well designed in general). This mainly stems from the fact that I wanted to learn the language while writing actual code, so in some situations I made bad design choices because of a lack of knowledge for the language (-syntax). In addition to this, my planning and design phase was too short to expect any kind of design worth a damn, but this project was as mentioned mainly to get familiar with php – not to design the best code or website on earth. Still, I would say that the source code structure in general is acceptable, it just needs some refactoring.

Functionality (Database Structure and Use-Cases)

A short overview over the implemented data structure as well as use cases:

    Entries which have a name, description as well as accepted status
    Categories which have a name, description
    A Category can have 0 to n Entries
    An Entry can be in 0 to n Categories
        List of all Categories
        Display of a Category and a List of its Entries
        Display of a single Entry
        suggest an Entry (which has to be approved by an administrator)
        add an Entry (which does not have to be approved by an administrator)
        view unapproved user suggestions
        approve user suggestion
        view all entries
        remove entry
        remove category
        add category

Additional functionality/implementation details:
    scripts for:
        inserting various test data (random, random lorem ipsum, xss)
        create admin
        creating necessary tables
        dropping all tables
        prevention of xss attacks as well as sql injection
    debug info:
        number of executed database queries
        list of executed database queries    

Php Project in action and source code

Here you can see the website in action (the scripts mentioned in Functionality are disabled, but the admin area is open) as well as download the php source code:

Please note that this code was my first attempt, and thus there are a lot of weaknesses. Most of the classes do a bit too much, and the security of the code is not good (tipps for you: use prepared statements, use bcrypt, know the difference between XSS and SQL injection and what escaping is used for which (also as said, don’t escape for SQL injection, use prepared statements), etc.). I still like the templating of the views (at least for a small-ish project).

php – triggerdb source [197.84 kB]
Login for admin area (if this will be abused too much it will be changed and not be published again):

PhpDoc: (although I must say, I was a bit sloppy on the documentation this time)

Abrupt ending

Soon, I came to a point where a functional (though slim) website was developed, but adding new functionality became a pain. This was mainly for the reason already mentioned above: the bloated controller(s).

I had to make a choice: continue (which would have meant to do some heavy refactoring) or stop and either give up, restart or look for some framework. I had the feeling that I got all I wanted out of the project (learning the syntax of php from the ground up as well as using sql and html in a real world situation) and continuing would be a waste of my resources. So I decided to stop. As I still enjoyed the experience and am now a lot more familiar with the general php syntax, I decided not to give up on php in general but to look for a framework which takes away the burden of doing tedious tasks. This led to me looking at the Frameworks Zend and Symfony (1 and 2), developing small websites in both and subsequent writing a tutorial on the Symfony2 PHP Framework.


As I am coming from an object-oriented (mainly java) as well as functional programming background, php was a new experience (as it does not rely on oop, though it is possible).

I am not becoming a fan of dynamic typing in an object-oriented language (or in any type of language for that matter), but I am learning to live with the disadvantages that come with it. Also the quite free mixing of php and html that is possible takes some time to get used to. One last point I have to criticize is the missing of an official documentation of coding standards/code conventions such as the Code Conventions for the Java Programming Language. Of course those will develop over time as one reads more code written by others, but until then some bad habits will have been developed which are hard to get rid of.

Some might consider all three points mentioned above as freedoms granted to the programmer, but I do think that too much freedom in programming (especially concerning dynamic typing) hurts code as well as design quality, raises the number of (undiscovered) bugs and – in the long run – raises development time. And I did not even mention the inconsistencies and overall design flaws in the PHP programming language. But still, I think PHP is worth at least trying out, and especially with a decent framework one can develop good, fast and save websites with it.

Leave a Reply

Your email address will not be published.