You are here

framework

PHP Frameworks, Part 2

I wrote a while ago about how I wanted to give up trying to develop my own frameworks because there were so many third party kits available and how they must be so much better than what I have time for. Well, I can't say that I've really pushed ahead with trying to learn any PHP frameworks. I've played with the two I mentioned but there remained a general feeling of unease - that somehow I'd be wasting my time and that I should move on to bigger and better things. That is to say, Java.

I have a need to develop a couple of webapps for myself and I am absolutely convinced within myself that these are best written in modern Java. There really is so much more you can do when you're in the JVM than in pretty much any other environment. Disclosure: my day job is mostly PHP and bit of Windows. I haven't dealt with Java on a day to day basis for many years now and I am completely out of date with modern frameworks.

So, I've spent the last few months slowly picking at Java and working out how to best write my apps. I've come to the conclusion that perhaps Spring and Hibernate as the best building blocks (not that I'm yet fully aware of what dependency injection is) and found AppFuse2, which is fantastic. AppFuse takes away a year of learning from getting started with Java.

But it's slow. I read the books, pick at the examples, look at forums. I'm still stuck. The learning curve is defeating me, even for someone who was totally immersed in Java half a decade ago and is a full time web programmer, albeit in PHP. Each obstacle I encounter doesn't just slow me down, it puts me off and makes it that much harder to get started again the next morning.

So on Saturday I made the decision to abandon the work I've done on my app and restart it in PHP. It's now Tuesday and I've spend ten hours working on it. I've spent much of that time building a PHP framework, but doing so for the needs dictated by my application (I find that unless something has a reason, it is difficult for me to concentrate on - this is true to me for maths too, where applications in physics and computing fascinate me but the pure form is an obscure cloud).

I already had a basic system in place: some objects, Smarty, controllers tying it together.. it worked. But I've rewritten it in vast amounts. Pretty much the only part that's left is the database abstraction layer, which is only because I don't see it as it hides under my ORM. (But I want to replace it: a) it's currently MySQLi only (give me Oracle, Postgres!) and b) I have new ideas even for this - see the PHP Optimisation post which describes a memcached aware database class)

This happens to me each time I delve back into Java. I never quite get there, but I take many ideas back with me to PHP. The future is created in Java. It really is. Look at any Computer Science research programme - you're going to find Java in there somewhere. I don't like playing follow-the-leader. I want to be there, at the head - I was once, but life got in the way.

But it pays to watch what the leaders are doing. This is true of programming as with anything else. I might not become the best, but I can become better.

So back to my framework. I've written several in PHP, both at work and for myself. At work, I do not innovate. It's a case of getting something done in the shortest possible time (giving the client what they want within their budget) and ignoring the long term possibilities (would you build a CMS using your own money then resell, or write something specific each time using the client's money?). That's how most hourly paid consultancy / outsourcing businesses work. At home, however, I am free to do my own thing. I am often more efficient, having time to think things over before starting work. I can plan, and not worry about who'll be picking up the bill.

My last 'home' framework pushed forward with using PHP classes. But it wasn't complete - a partially done MVC system used normal PHP procedural scripts to interface the view (Smarty) to the model (the classes). It wasn't correct, lacking use of interfaces and like, and to be quite honest being written in such a way that an interface never made good sense.

This has changed. The only bits of code not contained within classes are the PHP __autoload() function and a dispatcher. Interfaces define classes that can accept input from HTTP and generate output data (controllers). And then there's an interface for classes that can render that data - meaning that although I am still using Smarty, I don't need to. My renderer which provides part of the 'View' of 'MVC' is defined by a strict interface.

Classes now extend classes which extend classes ... My stack traces are growing; unheard of in my past PHP work. I'm applying Don't Repeat Yourself (DRY) and using well established patterns. The amount of code required to add a page or module shrinks with every modification I make. I'm abstracting the HTML forms down to ever-simpler arrays of meta-data. Classes can define dependencies which are injected into them at run time - despite PHP's typelessness.

There is a lot of the Spring framework in this, even though I perhaps don't understand Spring.

I really feel like I've accomplished something: indeed in terms of personal improvement, I have. I'd made more progress on my application in ten hours - including having written a generic framework - than I did in two months with Java where the framework was provided. But I still love you, Java.

I've also beaten a million times over the efforts of the last two years of my day job; the time over which I've been snatching an hour here and an hour there of client's time to adapt that framework to their needs and at the same time make something worth using for future projects. I worry now that I'll end up giving this code to my employer for nothing simply to make my days more bearable, as I have done many times before.

I have written a lot but said little so I'll wander off and do some more work I think. I will complete my application, which is really the whole point of this, then decided what to do with my framework.

I nearly wrote 'going forward' at the end of that last paragraph. I'm glad I didn't and that I won't be using that stinking phrase, going forward.

PHP Frameworks

I've finally become sick of writing my own frameworks for websites. I keep reinventing the wheel, and while I think I do some novel stuff I'm never going to match the breadth functionality available from frameworks developed by dozens or hundreds of people. Time is money and it's time to save (make) some money.

I've been looking at two frameworks:

ZendFramework

Pros

  • PHP5 only.
  • Modular enough to easily use within existing codebases.

Cons

  • Terrible documentation. I could not find one single example of even how to get started: I had to work it out from first principles.
  • Awful documentation.
  • Documentation leaves you reading source code to try to work out how to actually pass the parameters mentioned in the docs to methods.

Code Igniter

Pros

  • Easy to build complex sites from nothing and retain good practises.

Cons

  • PHP4 - no excuses now, PHP5 is essential. While 5 is far from ideal, PHP4's object model is unacceptable. Of course, CI being written in PHP4 doesn't mean that I have to write my code in 4 - but the very mention of PHP4 put me off enough to spend a day fighting ZendFramework before coming back for a second look.

Okay, there's not much here, but as I work through some projects I'll find things to say.

Saying that, anyone want to hire me for a project? Don't let this Drupal site with no effort spared "default theme and logo" look fool you, I am a real life web developer - I actually like writing business applications! Seriously.

Subscribe to RSS - framework