Working with multiple PHP frameworks – The best practice

Hi, first of all, I’d like to explain the title of this post. I won’t go into depths of complicated examples, but rather explain the logic of it.

PHP first implemented OOP (Object Orientated Programming) the right way in version 5. OOP is powerful tool in any programming language. But the next logic step was to create framework as an “abstract layer” that will help developers in both speed and quality of their work. To conclude this brief introduction, I’ll say that there are many frameworks written in PHP for PHP. 😀

Now to the fun part. For example I’ll use Magentos framework, that is written over Zend framework. To make stuff just a little bit more complicated. Zend framework is written on top of PHP’s built in functions.

Now, if you are a Magento developer (like we are 😀 ), you need to work by rules, or there will be problems with your Magento modules, extensions, etc. I promise. The problem is if you, lets say, use Zend framework directly in Magento module, which you can (Magento is its “extenstion”, which allows you to call parent methods). Why, you ask me? Well, to answer that, I will ask you a question. What will happen if you upgrade Magento to latest version which excluded some Zend framework functionality from its libraries?

It will collapse your module, and you will have to rewrite it in whole new way. But if you used only Magento functionalities, it will probably work just fine. If not, you can just take Magento functionality in one place, and insert it in latest version, so all your Modules that depend on it, work with “single strike”.

To conclude, you (and I) should never go “one level too deep” when extending a framework written in framework. Not only you will have less problems with updates / changes in parent framework, but you’ll need much less time to fix the problems that weren’t your problem initially.

A small example is in order, I think.

A class “Mage_Payment_Block_Form” extends some “Varien_Object” classes, but on top of that all, it uses Zend_Form, and its methods, to give you most usable class for our Magento platform. But if you use some of Zend_Form’s methods directly, like “setCaptcha”, and Varien decides to rewrite it in Magento (and exclude it from Zend library), all your modules that use that mehod will no longer be functional.

To make a long story short. Best practice would suggest that we all use only top level framework in our projects. Yes, it takes a bit longer (usually) to develop it, but at the end, it pays of big time. If nothing, when some of your colleagues start fixing their Modules, you will have your satisfaction. 😉

I hope this helped someone, and feel free to correct me if I’m wrong.