Why its good to write your own framework

 
Published on 2006-06-16 by John Collins. Socials: YouTube - X - Spotify - Amazon Music - Apple Podcast

Introduction

The PHP community online is massive. When Ruby on Rails appeared online and caused a storm of interest and hype, the PHP critical mass responded by producing scores of Model View Controller (MVC) web frameworks written in the popular language.

The subject of MVC frameworks is very topical for me right now as I am currently writing one for this website. One of the main dictums of object oriented design is that you should not "re-invent the wheel"? by spending time writing code which has already been written and is freely available elsewhere. So why am I not using one of the many freely available frameworks like CakePHP, Symfony, Agavi, or the Zend Framework? Here are my thoughts.

Learning a framework costs you time

While using a framework saves you development time, in fact this is the principal benefit of using a framework over traditional approaches to web development, learning a new framework costs you time. The more powerful and complex the framework is, the more time you need to spend picking through the documentation to learn it.

From my perspective as a freelance PHP developer and commercial Java developer, I know that I only really obtain a deep understanding of code that I have written myself. A framework written by somebody else is effectively legacy code to me, because I have to learn all about that code and which methods it offers before I can start to use it in my own project. If I wrote the framework code however, I implicitly understand the functionality that it offers.

Your framework equals your functionality

I am really impressed with the functionality of the frameworks on offer from the PHP community, and I also love the streamlined approach advocated by Ruby on Rails. In my opinion, Ruby on Rails did the PHP community a huge favour by provoking such a renewed interest in MVC and other design patterns, causing the PHP developers out there to take a fresh, serious look at the advantages offered by object oriented design and the new features offered by PHP5 in this area.

However no framework is ever going to be perfect, other than the one you design yourself because it suits your specific needs. For example for this site I wanted to design a new publishing system that offered facade interfaces to the Markdown, Geshi, and FPDF libraries to cover all of my text formatting and PDF generating needs. Furthermore I needed to build some HTML and PDF caching functionality around these libraries which was lacking in all cases. These requirements are very specific and are not covered by any existing framework that I could find.

Coding a framework makes you a better programmer

While working on the framework for this website, I have been exposed to many of the widely used object oriented design patterns, like the observer pattern, the facade pattern, the model view controller pattern, the front controller pattern, and the unit of work pattern. I have learned to implement all of these patterns within the confines of the PHP4 object model, and have also implemented my own data mapping layer with optimistic locking of table records. Why give somebody else the fun of learning all of this?

Like most other disciplines, the only way to truly learn how to program is to do it. Sure you might be doing something that has already been done by hundreds of other programmers before, but how else are you supposed to learn?

And now for the bad points

I began this article by stating that conventional wisdom dictates that you should not re-invent the wheel by reproducing the same work which has already been done many times before. This is generally sound advice, especially for commercial software development in large teams, where it really would not make any sense at all for each developer to have their own framework developed from scratch; therefore each developer will have to learn a central, standard library framework maintained by all. For new developers this central framework will always be legacy code representing an initial learning curve, but as commercial developers we have to live with this.

Developing a framework in splendid isolation, which has been my experience for version 5 of this website, is fine if nobody else will ever work on the code. If they do, for example if you work in a distributed team or if you intend to allow others to use your framework by releasing it, then you will need to document everything to do with your framework to enable other developers to pick through its complexities. Naturally this represents an increased workload for you, but tools like phpDocumentor will help you with this work.

Another drawback of designing your own framework is the time involved in doing the initial implementation, as I have learned with the development of version 5 of this site. The payoff for all of this initial work however is the knowledge that you can reuse your framework code again and again on future projects, which will save you time and work in the long term.

Conclusion

There are many, maybe too many, excellent web frameworks out there from the PHP community. I would recommend that you take a look at all of them as they each offer new, innovative ideas. If you care about clean object oriented design and agile methods, I would also recommend having a serious look at Ruby on Rails.

If like me you want to really learn about the concepts and design patterns behind these frameworks, then you might consider implementing your own. While not a task to be undertaken lightly due to the time and effort required, it will definitely make you a more accomplished programmer and will do wonders for your employment prospects.


Updated 2021 : note that the above post is out-of-date, given this post was originally published in 2006, but is left here for archival purposes.