The Art of Application Design in PHP
For years, web development has been thought of as the little sister of desktop programming. This can largely credited to the to the lack of design required, or even suggested to write any type of application.. In these days, one can pick up a language, such as PHP, and be able to write something significant, with next to no knowledge of the language, or application design. This article is my attempt to bring the art of application design to those who read. It is my opinion that such a practice should be with all applications you write, and it should be done right.
This article will not contain code examples. By me not posting code snippets, it forces you to read, and comprehend the concepts described here, rather then just copying code mindlessly. I hope this article helps you learn/expand your knowledge of application design, as it applies to PHP.
Throughout this reading, we will be discussing many things pertaining to object oriented programming. It is strongly advised that you have background knowledge on this subject. That being said, let's get started.
Why Application Design?
Application design is a key factor to writing successful code in any language; PHP is no exception to this. Those who understand application design, are able to write better, more efficient, robust code. It is because of understanding application design, that programmers are able to make such drastic changes to their programs, with no error. Can you change the entire layout of you website without ever touching your code? How about changing the way databases queries are handled on all pages, by editing only one file? If you answered yes, I solute you, but I still suggest you read this article. I'm sure it will contain new ideas you may never have thought of.
Design Patterns.
What's a design pattern? Well, I'm glad you asked. A design pattern is a widely accepted formation of code, that is proven to solve broken design concepts. These patterns, relate directly to object oriented programming. If you program in OO regularly, you're probably using many design patterns without even knowing it. This is because there is nothing special about these patterns. They are formed from the mind of a programmer, just like yourself. However, they have had extensive research done upon them, to prove they effectively solve a specific problem. There are many patterns publicly known today. If you would like to research design patterns, a Google search will do you well.
The Problem
In this article, I would like to specifically discuss one problem that plagues the web today. This problem is one of complete miss-design. Many pages online to day suffer from this problem, and don't even know it. 80% (figurative guess) of websites are guilty of mixing all of their content together in one page. They embed xhtml, database calls, and program logic together in their pages. “But wait,” you say ”PHP was designed to allow this. How is it a bad thing?” Lets explore this.
Have you ever wanted to re-design your website, but held off in fear of all the work? This is the case with many websites today. Web 2.0 is here, and with it came web standards. Sites that were created before the era, are often riddled with invalid XHTML, and neglect to fix such errors, due to the tremendous work load involved. It's a terrible issue for web developers to deal with, and it's all due to bad application design, and it is why such a concepts needs to be enforced in new web applications rising to meet the web.
The Solution.
The solution, is good application design. If you design your PHP wisely, you are able to make drastic changes to how your site looks, feels, and functions, without fearing some new bug popping up. Mind you, bugs will always exist, but by designing your PHP based applications properly, you remove many chances for a bug to creep in. Let's take a look at the “proper” way to structure your code.
As you may have gathered already, my prime concept here is separating output, logic, and data, where the output is XHTML, the data is input from the user (MySQL, forms), and the logic, is obviously, any business logic that requires your program to “think”. This is often a hard concept to grasp for new programmers, but one you must grasp none the less. Most people are able to separate output from logic, but run into trouble when trying to separate all three from each other. For such a system, we should separate the three different areas into “layers”(/objects), thus having a presentation layer, a data abstraction layer (DA layer), and a logical layer. One key thought to keep in mind when designing such a system, is the following guidelines:
1) The presentation layer should have no knowledge of the logical layer, or the DA layer. For all it cares, you are directly calling methods with parameters.
2) The DA layer should not have any knowledge of the presentation layer, or the logical layer. For all it cares, it's working on a desktop getting information for an instant messenger client.
3) The logical layer should not have any knowledge of how to present the output.
4) The logic should have no knowledge of how to get the data.
If you can comprehend a system with these specifications, you are well on your way to having a beautiful design.
So, where should we start designing such a system? The logical layer is always a good choice. The reason for this being that the logical layer is the only layer that has knowledge of the other layers existence. Remember, the DA layer and the presentation layer have no knowledge of each other; they work completely unaffected by one and other. Therefore, it only makes sense one would start with the logical layer.
The logical layer contains any and all program logic. It is essentially the page, with calls to the presentation layer, and the DA layer. How you set up this layer is completely up to you, though the MVC design pattern, which this article is being based off of, has each page assigned to a method. This allows for easy, clear separation of pages, and allows an entire group/category(essentially, the object) of pages to be looked at relevant to each other, with one single file. A new group can be created simply by creating another object, and a new page by creating a new method. This method of constructing the logic layer is strongly recommended.
The next step is a toss up bet ween the DA layer, and the presentation layer. Beings both are separate from each other, it does not matter which you tackle next. For this article however, we will talk about the presentation layer.
The Presentation layer is the layer responsible for displaying all data your site generates. Nothing should be returned to the browser that is not a result of this class (with the exception of PHP errors). This layer can often be referenced as the “templating engine” in many cases. It deals with templates, and makes sure no invalid data gets out, and also makes sure that the logic layer does not have to directly interact with any template files, or output. Remember my comments about changing the entire layout of your site? It is the presentation layer that allows for such actions. Because the logical layer never touches a template file, you can change the content of the template's all you want, and need only change the presentation layer (if even required at all).
This layer needs only one copy, unlike the logical layer which can have many for each different group of pages. It is also recommended that you have a layer to interface with user input (ie: forms). If you wish not to create a separate layer for it, this would be the perfect layer to include it in (though I recommend a separate layer).
// MustyWindows - Jump Through The Windows
// AmpFusion - Where Underground Becomes Mainstream
Neo Enterprise Technologies Coming soon.