So, I’ve spent some time upgrading the darwingames.com codebase to be PHP5 compatible. For those considering the transition (and language designers who are considering an incompatible source version upgrade), I offer the following observations.
The Good:
Relatively few files and/or lines needed to be changed. The major “classes” of changes were:
class var => class public/private/protected
This is a straightforward change due to the new scoping capabilities of PHP5. For bulk changes like this, making the scope public will match the default behaviour of var in PHP4.cannot modify $this
We had used the idiom of:
$game = new GAME();
$game->load( $id );
…which modified this:
$this = unserialize( getData($id) );
Now that static functions are available, it’s simply easier to use the factory-method idiom as they recommend.
$game = GAME::fetchGame( $id );
static methods
I know that you could previously call methods viaMyClass::myFunction();
, but now that it feels more officially supported, it gives you more options and makes PHP feel more like a “real” language (that and:funcResult()->objectMethod();
call support!).random
=&
stuff
Now that objects are passed by reference by default, most usages of=&
and&function foo() {...}
are unnecessary.
The Bad:
Most of the “PHP5 changes” taken individually really do make it a better language. However, there are some significant problems and barriers faced by PHP5 due to some of the decisions the implementors have made (remember, this is from a user’s/programmer’s perspective).
E_STRICT
I program with E_STRICT. No if’s, and’s, or but’s. PHP5 (or any PHP) with E_STRICT is nice because it can catch a lot of silly “-ism’s”, misspellings, bad habits, etc. That’s not the problem, the problem is how it interacts with existing PHP4 code and the PHP5 language changes…var => public/private/protected
I know I said this was a good change above, but in the context of PHP4 and PHP5-E_STRICT, any existing class withvar
keywords (ie: every single PHP4 class) will not run on PHP5 using E_STRICT without changes. Ok, no big deal, but that leads us to the next point…PEAR and/or other third-party libraries
Meaning that every library or third-party library must make a choice between being PHP4-compatible or PHP5-compatible. That’s a fork, and it’s pretty difficult to maintain something that’s both PHP4 and PHP5 compatible, dealing with STRICT-mode in both versions. Furthermore that’s a decision that’s tough for a project or module to make (support 85% of existing installs or go for the “new-hotness”), and it’s not a decision that is easy for you (an individual) to make yourself. Message to PEAR. Fork now. Pour all resources into the new-hotness (PHP5) and freeze features in PHP4 versions of the libraries.is_a()
vs.instanceof
Ok, so you removefunction is_a()
and make it operator:instanceof
, life is good, less things to bugfix, etc. But WHY do you makeinstanceof
not take a string as a paramter (only a$variable
or actual class reference). I guess a minor issue in the long-term, but really annoying in the short-term.
So, when designing incompatible language changes let me offer this advice:
Take into account third-party libraries, they have 99% more code available than the average coder will write in their lifetime.
Have a transition plan in place (that takes into account third-party libraries)… ie: run E_ALL - E_STRICT for six-months, update silly syntax-changes, and then switch over after
now() + $SIX_MONTHS
.Consider an E_PHP4_STRICT / E_PHP5_STRICT (or more generically: E_ALL_STRICT, E_COMPATIBLE_STRICT) to make maintaining PHP4/PHP5 code-bases easier what also might be nice is to do something like E_STRICT_ONLY_ON_NON_INCLUDED_FILES, as I’m perfectly OK with errors in included files, but I’d like to tag my own code as ‘needing to be strict’.
Having only browsed through the PHP codebase a few times in my years of using PHP, I am totally unqualified to make sure these recommendations are feasible, but from a user of the language’s perspective, they’d be items I’d appreciate (and if this experience report helps you in your PHP4-PHP5 transition, I’m glad :^).
22:46 CST | category / entries
permanent link | comments?