5 PHP 5 features you can’t afford to ignore
by Marco Tabini · April 27, 2008
There are dozens of reasons to switch to PHP 5—not least the fact that, if you’re still stuck on PHP 4, the PHP team is about to pull the rug from under your feet. Despite the fact that you may not have a choice in the matter, upgrading comes with a number of bonus new features that can help you write better code and gain access to new functionality that required a fair amount of hacking in previous version. Here’s a quick list of 5 personal favourites (with a little help from my Twitter contacts):
1. SimpleXML
Although its name is the source of much frustration—simple is such a relative concept—SimpleXML is a great extension that makes manipulating XML data a breeze. Thanks to its integration with the PHP 5 object model, SXML allows immediate and intuitive access to the contents of an XML document using normal language constructs: your XML document simply becomes a hierarchy of PHP 5 objects that express elements and their attributes. Combine with XSLT for a kick-ass templating system that (a) is based on well-known standards so your development team doesn’t have to learn some made-up markup language and (b) really forces you to separate your frontend from the rest of your application.
Bonus features: SXML integrates with Xpath, making complex XML data extraction practically trivial. If you SimpleXML won’t do something you need it to, its underpinnings are based on the same library as DOMXML, making swapping between the two a breeze.
2. JSON and SOAP
Web services are all the rage, and blah blah blah. Forget the hype—the reality is that no serious web application can afford to have a single frontend anymore. Your users expect to be able to interface to your code in a multitude of ways and, if you allow them, will come up with new ways you haven’t even thought of.
That’s why the support for web service functionality built into PHP 5 through the JSON and SOAP interfaces is a boon for developers both because it lets them consume external services—making rich functionality easier to achieve and less expensive—and because it allows them to create interfaces accessible to anyone. Even more importantly, these interfaces make PHP a viable choice for complex backend applications that can be accessed directly from all sorts of Rich Internet development environments like Ajax and Flex.
Bonus functionality: some PHP IDEs (like Zend Studio) are capable of generating WSDL files directly from your PHP source code (thankfully, because writing WSDL files is about as fun as getting a root canal done with a rusty drill). Large vendors, like Adobe and Microsoft, are taking notice of PHP 5’s new interoperation capabilities and actually investing in making their products compatible with it and promoting its usage.
3. PDO
I am not entirely sure why, but PDO seems to be one of the great mysteries of PHP 5 (although not as much as SPL—mentioned later in this entry). The idea of accessing multiple databases using a single library is nothing new, of course, but PDO takes an interesting approach to the problem of database abstraction—it only abstracts access to the database, as opposed to the entire interface to it. This means that it doesn’t force you to adopt a “lowest-common-denominator” approach in an attempt to make your application function with every possible database, or implement costly abstraction layers. Rather, it allows you to access your database of choice using a simple, consistent interface that promotes good coding practices and provides important functionality like prepared statement and built-in escaping without bogging down your code.
If you are used to DBMS-specific libraries, PDO may seem a little hard to justify until you start using it consistently and find yourself wondering how you managed to live without prepared statements and language-level iterable result sets—to name a couple of the multitude of new features introduced by it.
Bonus features: drivers for all the “popular” DBMS, including MySQL, MSSQL, Oracle and more. Proper, exception-based error reporting.
4. SPL
If I had to pick a favourite-of-favourites in the new features provided by PHP 5, I’d go for the Standard PHP Library. The SPL allows userland code to present itself as one of a number of built-in PHP language elements like arrays. This sounds like little more than a triviality, but the overloading functionality that it provides can be incredibly powerful. The ability to make any object behave like an array—including iteration using foreach()—is perfect for result sets, filtering functionality, and so much more.
SPL is still suffering from a lack of solid documentation, but its functionality is relatively easy to understand, and some articles on its use do exist.
Bonus features: SPL comes with a number of useful built-in classes, such as DirectoryIterator, which makes examining the contents of a directory tolerable, and SimpleXMLIterator, which ties beautifully into SimpleXML. It also implements an enhanced autoloading feature that simplifies dynamic file inclusion considerably. Recursive iterators make for wonderful geek jokes.
5. SQLite
How many times have you wished you could have a cross-platform, portable, powerful, easy-to-use, file-based database system? Perhaps never—but only because you couldn’t use SQLite. This simple but full-featured DBMS packs all the punch of MySQL in a format that doesn’t require a database server and runs on every platform on which PHP 5 can be compiled. Creating databases is as simple as creating a file, and everything else follows an almost-standard SQL syntax that provides practically everything that you would expect of a modern DBMS—including support for transactions!
SQLite is a perfect choice for data storage where a MySQL database is overkill and a simple text-based file format doesn’t provide the performance you need.
Bonus features: SQLite columns are weakly-typed, making it possible to store just about anything. You can also register your PHP functions within the extension and access them from within your SQL statements. SQLite 3 is widely supported by a number of developer technologies, like Flex, making interoperability a breeze. A nice, multiplatform, GUI-based SQLite database editor built using Adobe AIR is readily available.
And much, much more
So, there you have it: five very good reasons to embrace the move to PHP 5. Any one of these five technologies has the potential to revolutionize the way you write code and open up new opportunities for your development efforts.
This is by no means a comprehensive list. Do you have other favourites? Share them below.