As the release of php 5.3 is nigh, let’s take a look and see what the php guys have messed up erm.. improved this time. The world of the interpreted langues had passed by php a long time ago, can this old dog learn a few new tricks? I hope so. I work with php on a daily basis, and I don’t like it – its current state. Anyone who have written some real world ruby or python code can see what I mean – php just “sucks”, it feels heavy and bound, sometimes it’s just a pain in the ass. This of course doesn’t mean php isn’t one of the best platforms for web development… yes, I say platform, not a language. Anyway, let’s see what we’ve got here.
First, we get namespaces
Aye, say goodbye to lengthy, domain-prefixed function names. We won’t need to say couchdb_fetch_dbs() again, instead we’ll write namespace CouchDb and then CouchDb::fetch_dbs() woot! This can also reduce class name clashes. Yes, namespaces can be embedded, so we can have Oh::My::God::this_is::deep. Wait, wait. Are we sure Oh::My::God::this_is::deep::iamanicefunction_whydontyoucallme() is a step in the right direction? The ‘use’ keyword comes to convince us that it is: as in any decent, namespace-supporting language, we can ‘include’ any part of the namespace (watch out for clashes though). So just say use Oh::My::God and then God::this_is::deep::iamanicefunction_whydontyoucallme() and there you go. You can even import single classes. I mentioned clashes above – what can we do about them? Hey, php seems to get more intelligent this time, and it supports namespace aliases. Cool. We now can say use NamespaceOne::User as FirstUserClass and use NamespaceTwo::User as SecondUserClass. Namespaces aren’t for classes only! We can define functions inside, too. Also, we have the global namespace – ::global_func() , which is useful if we want to call a global function from inside a namespace which has a function with the same name.
So, a quick summary:
- Namespaces of depth
- Importing namespaces/classes
- Namespace aliases
- Namespaces aren’t limited to classes, functions also work.
- Use the global namespace, Luke!
Then we have late static binding
What the hell is that?! Consider a simple example of overriding the parent’s static method:
class IamyourfatherLuke {
static public function SayMyName($whoami) {
self::do_the_speaking($whoami);
}
static public function do_the_speaking($what) {
echo "Darth Vader says: {$what}n";
}
}
class ThisisLuke extends IamyourfatherLuke {
static public function do_the_speaking($what) {
echo "Luke says: {$what}n";
}
}
ThisisLuke::SayMyName("hello Leia");
Now, what will this print? Easy: “Darth Vader says: hello Leia”. Wait, what? Hey, I’ve overridden the ‘do_the_speaking’ method! Aye, you did, but forgot about that tricky little ‘self’ prefix in the original class! ‘self’ resolves to the exact class the reference is used in. And it’s used in the parent class. What if you really want to override such things? Well, firs, wait for php 5.3, and then say static::do_the_speaking($whoami);
We also get a native mysql driver
Of course we will still be able to use the good old libmysql based extension, but why not try a zend-optimized, php specific one which at least will obey the php setting regarding maximum memory size and also use the zend engine’s memory management system?
Oh, yes, and class constants
Need I say anything about this? This is clearly a gem in the pavement to better system design.
SQLite3 support built in
Nice. Perfect for starting small and even sufficient for some mid-scale stuff.
My personal favorite: E_ALL now includes E_STRICT.
Nothing compares to the beauty of conforming unspoken specifications. Also, E_STRICT has been broken apart to E_STRICT and E_DEPRECATED. Nice.
I can hardly wait for more goodness, like anonymous functions and closures, but sadly that’s a long way from here. PHP seems to be making baby steps. Actually, baby steps lagging years behind.

Facebook
Google
LinkedIn
Twitter
RSS
Pingback: ??????? » [Web] ????