Why Code Revolution Has Adopted Use Of Zend Framwork
July 26th, 2010 | Published in PHP Development
So this is going to be my first post about Zend Framework and why Code Revolution is slowly adopting its use in many of our non CMS projects. Hopefully this article will provide you with a few insightful tips and tricks within the framework and also give you an idea of why it might benefit your development process.
The Original Programming Problem – Spaghetti Code
I’m sure a lot of you PHP programmers have heard that dreaded expression “Spaghetti Code”. But what exactly does this mean? For me in the past, Spaghetti Code simply means a code base that’s become unreadable, unmanageable and most of all makes re factoring very difficult (more on re-factoring below).
As a website developer, our aims are always simple. To build nice looking, functional and secure websites for our customers. A novel concept and one we take seriously at Code Revolution. But what happens once you have built that clean, lovely looking site?
In my experience that’s never the end of the story. Business requirements change, new features are requested, products are added. That original code base you put together is quickly expanded and quite often, at the expense of good programming standards. Before you know it, you really do have “Spaghetti Code”. HTML code is embedded in PHP strings, Database Queries are being called from all over your code. You open a PHP file but no longer understand its functionality at first glance.
The above problem is a common one and one I’m sure every good PHP developer worth his salt has come across. In fact its a problem experienced so often that as PHP development has evolved to try to combat this code decay – welcome MVC architecture.
The Model View Controller Architecture
Now its possible you have heard the buzz word MVC on various sites that also talk about PHP code. It has somewhat become a kind of guru buzz word for programming purists over the last couple of years. Fear not though, MVC is not as complicated as it sounds and as such, ill try to give a simple example below.
Model View Controller basically refers to the 3 common areas that a PHP project can be broken down into. Try and imagine breaking any one of your own recent projects into these categories;
Model – This is where your data resides, quite commonly in a MySQL or SQLite database. The model is the data access layer for moving your data in and out of a storage medium.
View – The most obvious one, this is the part of your application users can see, this includes your rendered HTML, CSS files and such like. It can also represent other client data such as RSS feeds, XML, images.
Controller – The bit that does the grunt in your application. A controller in very simple terms handles a request between the view and the model. In other words a controller would for example bridge a view showing some database content to the Model or database where your data is residing.
So why break your application down into its various segments like the above? The main reasons are;
- Maintainability
- Extensibility
- Code Reuse
Admittedly that’s a very simplistic view of what MVC is but then this article is intended as a simplistic explanation. Next lets talk about Zend Framework and how it uses MVC.
Who is Zend and What Is Zend Framework?
Ok for those of you, Zend are the official company of PHP. They are a company based in Israel who promote the development of the worlds favorite web programming language. They offer training courses, IDE software, PHP server tools and much much more. Whereby PHP is a open source programming language, Zend is PHPs commercial counter-part if you like.
So what is Zend Framework then? Zend Framework is a MVC based PHP framework that encourages you to build your applications in a true MVC architecture. It is also a collection of highly tested, fast and reusable code modules that allow you to do many of the common things a standard website would do.
Put simply, Zend Framework encourages MVC design and cuts down your development time drastically by offering you the use of many Open Source modules. Zend Framework is based on PHP5s Object Orientated programming architecture. Code Revolution have adopted use of this framework simply for that reason, it offers a lot of pre-written and pre-tested code for you to use straight away.
An Example of What Zend Framework Can Do
Lets take an example website that has a small shopping cart system for buying badges. Lets think about some of the things our site would need to do;
- Connect to a database to get product information
- Display those products on screen as a list or table
- Collect user information for purchases and save it to a database
- Email customers with information, receipts etc
Think about if you were a developer writing this from scratch. You would have to write all of the Database connectivity. Create all the forms in HTML yourself and use PHP to handle, sanitize and filter the data. You would have to write or port your own SMTP email system.
Now take Zend Framework. It has the following modules that with some simple code calls using OOP, will do a lot of that for you.
Zend_Db – Database Abstraction and Connection (Model)
Zend_View – Display Content Cleanly and Simply (View)
Zend_Mail – Send Emails over SMTP, PHP Mail and Many Others (Controller)
The great thing about using Zend Framework too is that the license is GPL, meaning you can use the framework completely free. It boosts your development time and also helps you use pre-tested code in an MVC way. Your code then remains simply structured whcih makes it easy to refactor (re-factoring is the continual adjustment and amendment to a code base over time).
As we mentioned Code Revolution intends to continue to adopt Zend Framework for all its new projects. We have found it to be a useful addition to our development tools and processes.
I hope you may find the above article a nice simple definition of Zend Framework. Please feel free to leave any comments.



