Don’t be afraid of PHP 5.3

Posted March 2nd, 2010 by Juozas

While attending PHPUK conference in London, I noticed how much talk there is about PHP 5.3 and “when to upgrade?”, there was even a presentation about that. Because I have been using PHP 5.3 for more than half a year, I decided to share my views on this topic. This topic is very important as the earlier PHP 5.3 is adopted, the sooner second iteration of frameworks can be released (story for Symfony).

New features

PHP 5.3 includes a lot of new features. Some of them are new language features like namespaces, closures, exceptions chaining, jump labels etc., and even though these are very recommended to learn, they are not required. You can continue to write code as you are used to and chances are that it will still work (more about that in Legacy code).

What is important from new features is fixes are enhancements. For example new garbage collector and other fixes reduce memory usage and increases performance. There are also some new extensions like phar, sqlite3 and fileinfo and some libraries (like mysql or openssl) have some major improvements. Finally, Windows support is improved even further and now is expected to be even more production-ready.

Legacy code

Apparently the biggest problem is with legacy applications – somehow everyone just decided that because of all these new features, everything should break. If you’d look at backward incompatibility list here, you notice that not that much has changed. Or in other words – if your code was “correct” before upgrade, it will work fine with PHP 5.3 too. For example static and/or private setters, ereg and some more other features shouldn’t have been used in a first place as manual clearly stated that.

Personally I have upgraded around 10 projects to run on PHP 5.3, each took less than an hour. On my development machine I also have strict error reporting level so I even had to remove marked-as-deprecated code, but for larger part of code everything worked fine. Furthermore, some of that code has been written using PHP 4, a couple years ago, and it still worked just fine.

Debian…

As far as I’m aware of, main linux distros like Debian or Red Hat do not have PHP 5.3 in their stable repositories, which makes upgrading potentially very hard or impossible. I have come across this before – sysadmins do not want to install anything outside from stable, so the only option is to wait (that happened to me when PHP 5 was still not well adopted). This is probably the only reasonable argument to not switch to PHP 5.3, but only if you don’t have option to switch servers.

I’m lucky with this problem, because I do not administer servers, but still can request for any PHP version I want. If you do not have this option, maybe it’s time to switch hosting server provider? There can be different situations, but software version shouldn’t be a limiting factor, especially when new version is available and known to be stable enough. You can even use multiple PHP versions if switching server would require testing a lot of applications.

Why you should be using PHP 5.3 right now

Firstly because it has those new features, which mean that you can start to use them as an early adopter and benefit later. All second iteration frameworks (Zend Framework 2.0, Symfony 2.0, Lithium etc.) coming out late this year will use PHP 5.3 extensively, so knowing how namespaces work (I’d say they are the ones you need to look first) and how to utilize closures will mean that you can get going very quickly.

Doctrine 2.0 is probably the best example of why you should learn PHP 5.3 now – most of its features could be written in older versions too, but not as clean and easy to understand. And it’s already in alpha stage, so stable version might be coming out very soon and yet again – if you want to stay on top of your game, you need to know how it works and how to leverage all new features.

As mentioned above, because of new garbage collector implementation PHP 5.3 performs much better (benchmarks). From my personal tests I have noticed that Zend Framework uses much less memory, just because there is less memory leaks, Doctrine of course gets a big speed up too.

Conclusion

I’m yet unsure what is all that buzz about – PHP 5.3 is stable, way faster than previous versions and works with legacy code just fine. Considering that it has new core features, which will be expected from developers to be known in just a few months, I don’t see a reason why you shouldn’t use it. And if you have any questions you can just ask me directly on twitter.

Trackbacks/Pingbacks

  1. Juozas Kaziukenas’ Blog: Don’t be afraid of PHP 5.3 | Development Blog With Code Updates : Developercast.com
  2. Upgrades In Open Source | BrandonSavage.net

Comments (13)

  1. Court

    Well said. I think it is important to note that if you code in E_STRICT (which there is absolutely no excuse not to do so) and you were running PHP 5.2, there should really be nothing that needs to be changed.

    Regardless, all developers that use frameworks should make the upgrade long before the new versions are released. It is hard enough to adapt to new language features — don’t make it harder on yourself by trying to do it all at once.

  2. Tom Boutell

    I would like to use 5.3.x but 5.3.0 had some serious showstoppers for production use. Many of those got fixed in 5.3.1, but I continued to experience some segmentation faults – not PHP-level errors – in complex PHP code with no real trail of breadcrumbs to figure out the cause. I realized I had other work to do and rolled back to PHP 5.2.x, with regret.

    Perhaps I’ll take the plunge again soon, especially if PHP 5.3.2 gets out the door.

  3. monk.e.boy

    Tom,

    We had that too, some random pages using ZF crash, put some sleep(0.5) in the action methods and everything works OK. Ha. Nice.

    So we report the errors but PHP take no notice and just close the bug report. Nice one…..

    monk.e.boy

  4. Brandon

    Great read. I suspect I will be butting heads with my IT guys about rolling this out in the near future, this will give me some ammo.

  5. Mario Awad

    Same here. I put PHP 5.3 to the test a couple of months ago and ended up having lots of trouble with XDebug under Windows XP. The debugger crashes randomly with no specific reason. Switching back to PHP 5.2.x was all I could do. Maybe when second generation frameworks come out it will be a good time to give it a second shot. Thanks for writing this good article and I which such errors never occurred. Cheers.

  6. Mario Bittencourt

    I agree with the migration but unfortunately there is always the case of legacy code that does not work with 5.3.x because the register_globals no longer exists :)

    Seems that the answer is create a virtual machine with 5.2.x to put all those apps that rely on this until they die.

  7. Lesya

    It was interesting to read this article and I hope to read a new article about this subject in your site in the near time.

  8. Chris

    I suppose when 5.3 finally hits stable release I’ll be sue to try it out. Until then, they should perhaps continue to call it beta# or RC# instead of actual releases; 5.3.1 and 5.3.2 have strange issues indeed.

  9. Juozas (author)

    Strangely crashes never occurred to me. Xdebug had some problems but these have been fixed and production server is running rock solid :)

  10. Kjartan Ólason

    I made the upgrade without even noticing any difference. I have a very very very large project with thousands of files and millions of lines, and I didn’t notice any difference at all.

    If someone had told the to be scared of 5.3, I wouldn’t have upgraded, but I thought it was just a minor upgrade, with no breaks.

  11. Pieter

    We’ve been running on php5.3.1 for a little while now with our heavier zf based projects and all seems well. The only thing we noticed is that some methods have been marked as deprecated for which no alternative exists except in a pecl extension.

  12. Slavi

    I recently installed Fedora Core 12 and it had php 5.3 by default.

  13. dragon

    No zend optimizer for php 5.3 . So it is a serious reason for me :(

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">