Learning LAMP web developement – 6. PHP project organization, framework, libraries and third party code
Source code re-usability is one of the most important things if you plan to keep developing applications for longer. All Internet applications have set of functionalities that are common. These are for example, User management, permissions management, CMS functionality, forums, categories, menus, profiles etc. Therefore you don’t have to write a lot of code from project to project again and again if these functionalities are written in form that they can be reused. There is no doubt this saves one a lot of time in the future.
Source code and functionality needed for projects in general we can put to something that we can call application framework. There are already several frameworks you could choose from to start your project or you can think about to design your own.
I think after many years of development programmers end up in quite similar way of project organization. Most of the frameworks declare they are MVC based. I ended up myself with something very similar to MVC also. This is actually separating code which handles data (Model) code producing resulting HTML pages (view) and code making decisions what to do and when (Controller).
Here are my suggestions of how project could be organized.
Library – here you keep general project independent objects. You can just take them from project to project and use them. Third party or your own, doesn’t matter.
Local Library – here you put all objects which are project specific and makes no sense to take them out to other projects. Here are also object manipulating data in database.
Templates – here you keep your presentation layer files e.g. templates which are being parsed by system to produce resulting content sent to browser.
Modules – here you put all decision making objects (controllers). Modules are PHP objects receiving user actions and requests, they ask data objects to process or provide data and parse template with them to produce resulting web page. Modules can represent part of age or entire page.
Cron – all scripts for automated processing are located here
htdocs – and finally the document root folder. This is place where your server is configured to call index file from when your website is accessed. Note that all other project files are outside of this folder which make then inaccessible from web browser so all are secured from public access. To this folder you put your index.php file as execution point and public resources as pictures, js or css. All resources browser must have access to.
Some tips how to handle each type of these object we have here.
Library objects
You need some functionality that would be quite hard to write all yourself and there are plenty of solutions for these on the Internet. Image processing, Get decent class which handles various formats, does water prints, cutting, stretching all stuff you’d imagine and place it here. Do with others as well. There are plenty of libraries, try to look at pear.php.net for code object or pecl.php.net for extensions. What you should be aware of is that third party libraries may contain bugs and security holes. As they are open source anyone can find them out and abuse. You should be watching for upgrades of libraries you download and also verify source of libraries you use.
Also you should not modify libraries on your own as you’d loose option to upgrade them. If you need to modify or extend its functionality do it rather in your own object which will inherit from original and override or extend missing functionality. You can do this here or directly from objects in local library if you need change only for particular project.
As each developer needs from time to time some general functions not specifically related to any particular object, I recommend to create one general class here where you put all your global functions as static methods of this class. in code you than call them as e.g. my::convert_units(); by doing this you isolate your own public functions to own scope, you won’t confuse them with native PHP functions.
Local library
Each project is specific so it will need a lot of functionality. This is a good place to put such code to, as these objects are separated from general libraries. These are Models by MVC definition as they handle particular type of data. When creating these objects you should let one object to handle specific type of data e.g. user data in DB and user operations are handler by one object called user. Never put same data manipulation to 2 different objects as it would lead to code duplication.
Templates
Templates are usually HTML files mixed with some programming statements which are replaced by data when parsed by templating engine. There are various templating engines e.g. http://www.smarty.net/. Template engine helps to separate front end and back end programming. so they can be developed by independent people. To help front end designer to be able manipulate data without having to access PHP files, you should prepare debug output printing all available data structure when each module template is parsed. Front end designer then can easily see what he has available in template.
Modules
You can think of these objects as various parts of your website. e.g. Menu, header, footer, product listing etc. But the module is also something which can put all these together to complete the web page. Something that controls layout (HTML frame) of page. Module can have more templates associated which acts as “views” then. Design your modules to be flexible. Design them to be included to any template. Then what you’ll get is ability to place any part of active or static content to any other module. You’ll be able to build hierarchy, widgets, and on top level structure controllers.
Modules can also handle requests made by user on particular page, they control inputs and outputs, call data objects to store or get data from database, tell other objects to do something.
Cron
These are just php scripts with their own execution point. They should include your project settings and libraries as they should also just command data objects what to do, same as modules they are in some sense Controllers. Don’t forget nice and clean logging to files and error reporting as logs are only way to recognize whether automated script do what there are suppose to.
Htdocs
And finally we are at folder where public resources are located. These are files like pictures, CSS styles, JavaScript and other. Don;t put here protected files as you make them public. There is one important file which is worth to talk about which is main execution point of the website. Index.php file. Good things to do here are inclusion of configuration file located outside of htdocs. Loading libraries which can’t be auto loaded and it is also good idea to register shutdown function in which you output resulting content. It is not a good idea to directly output content to browser as you process it as you may need to inject some HTTP headers to browser which cannot be done after feeding web page content, these are for example redirection header of cookies or content type etc. So it is good to store content in output buffer before you output it to browser. good place to output it is shutdown function as this is called also in event of fatal errors and there you can output also your debug content during developing (back-trace etc.).
If you learn how to handle all these properly, and you keep code reuse in mind, you’ll end up with nice own framework which you can easily base new project on next time. Reusing what you’ve already written and saving a lot of time yourself.
Published by Stan Kuhn in: PHP tutorial
