Introducing P5 - A framework apart

Introduction

P5 is an extremely simple open source PHP5 framework, targeted towards beginners of PHP5. It allows new programmers using PHP5 to get the idea about basics of a framework, how it works and what can it provide in contrast to the classic method of development, where mindless copy paste logic is applied to create a rapid application, which overtime becomes hard to maintain and extend.

Basics

It's a sad truth that majority of PHP developers have no idea of a framework or what it does, specially beginners who kick start their development using a copy/paste/edit/refresh cycle, which produces the desirable output from the application.

P5 was developedas a working demonstration to show beginners how a web application development can be simplified, by breaking the application into small units of code. Each code unit works separately from the other, with least dependency. Moreover, each code unit provides an example of OOP concepts, which allows you to learn about object oriented programming in a relaxing manner.

Downloading/installing the framework

P5 comes as a zip package which you can directly extract and run from any folder inside your web document root. The whole source code is a mere 12 kb archive, with almost the equal amount of comment lines as the source code! There are only 5 core classes, an index.php and an .htaccess file.

Framework core

Each code fragment in the framework is explained clearly and is easy to understand from a beginner's point of view. Like other frameworks out there, it has core set of classes, which are independent of each other and provide one level of service to the framework. The index.php file implements the application life cycle code, and the .htaccess provide simple rules to create restful urls for the application.

The framework DOES NOT follow any particular pattern like MVC or HMVC, because it is not intended to provide one, however, you can make changes to the code to include a particular pattern in the framework, which is upto the programmer to decide based on his/her needs.

There is no documentation as all beginners are encouraged to read the index.php file and other core classes themselves. This allows them to learn few basic OOP techniques as well as good programming practices. Comments in the source code explain what's happening, and why something is done in a particular way.

Using the framework

You, as a beginner, should try and first read through the whole source code to understand and learn the framework first. It takes only about 15 minutes to go through the whole source. Once you are fimilar with how things are working, you can edit files inside the core/classes/ folder, which are inherited from the framework base classes. You should add your own methods and improve the existing one, as you develop your application. You will hardly ever need to change anything in index.php or the base classes. A few things to remember while you edit/extend the code:

  • Make sure you don't write a long method or routine and break it up into multiple functions/methods. Each method should do a particular task and return proper values
  • Add new classes to the classes folder if your require, to achieve a particular functionality. For example, you can create a Download class which handles all downloads from the application, instead of adding code to the Page class for that functionality
  • Try to avoid dependency between the classes you write. The more dependency between your application classes, the more difficult it will become for you to debug the code in case of problems.
  • Lastly, make sure you use the Log class methods as much as possible in your code, at various points in the application. This simplifies debugging if you enable all logging functions and also helps you understand the flow of code from one point to another.

We hope that our framework will be helpful to newcomers and they will be able to learn some good things from our code. Although the framework is hardly usable for any production use, it can provide the base for another larger framework, with more features and functionality. This entirely depends upon how you extend the framework and how far you go in your implementation.

Posted: 10 Jul 2011
Blog comments powered by Disqus