PHP 5 improves on PHP 4 in three major areas:
1-Object-oriented programming
2-MySQL
3-XML
These items have all been completely rewritten, turning them from limitations into star attractions. While these changes alone warrant a new version of PHP, PHP 5 also provides a plethora of other new features.
1. Robust Support for Object-Oriented Programming
Ever since Zeev and Andi's late-night OOP hack, PHP programmers have clamored for an increasing amount of OO features. However, neither PHP 3 nor PHP 4 truly incorporates objects into its core.
With PHP 5, PHP programmers can at last stop apologizing for PHP's krufty OO support. It offers:
1-Constructors
2-Destructors
3-Public, protected, and private properties and methods
4-Interfaces
5-Abstract classes
6-Class type hints
7-Static properties and methods
8-Final properties and methods
9-A whole suite of magical methods
Additionally, objects are now both assigned--and passed--by reference instead of by value, so the necessity to liberally sprinkle ampersands throughout your code is no more.
If you're a person who enjoys web programming using objects and patterns, then these features alone will make your year. However, PHP 5's just getting started.
2. A Completely Rewritten MySQL Extension
The MySQL database is PHP's partner in crime. Many developers power their web sites with MySQL, yet the MySQL extension is showing its age. In retrospect, some design decisions weren't the best solutions after all.
Also, the latest versions of MySQL, 4.1 and 5.0, introduce many new features, some of which require significant changes to the extension. As a result, PHP 5 comes with a completely new and improved MySQL extension. Dubbed MySQLi, for MySQL Improved. It offers:
1-Prepared statements
2-Bound input and output parameters
3-SSL connections
4-Multi-query functions
MySQLi even takes advantage of PHP 5's new object-oriented support to provide an OO interface to MySQL. On top of that, the latest versions of MySQL now enable subselects, transactions, and replication.
3. A Suite of Interoperable XML Tools
PHP 5 fixes the major problems in PHP 4's XML extensions. While PHP 4 allows you to manipulate XML, its XML tools are only superficially related. Each tool covers one part of the XML experience, but they weren't designed to work together, and PHP 4 support for the more advanced XML features is often patchy.
Not so in PHP 5.
The new XML extensions:
1-Work together as a unified whole.
2-Are standardized on a single XML library: libxml2.F
3-ully comply with W3 specifications.
4-Efficiently process data.
5-Provide you with the right XML tool for your job.
Additionally, following the PHP tenet that creating web applications should be easy, there's a new XML extension that makes it simple to read and alter XML documents. The aptly named SimpleXML extension allows you to interact with the information in an XML document as though these pieces of information are arrays and objects, iterating through them with for-each loops, and editing them in place merely by assigning new values to variables.
In short, SimpleXML kicks ass!
If you know the document's format ahead of time, such as when you're parsing RSS files, REST results, and configuration data, SimpleXML is the way to go.
And if you're a DOM fan, you'll be pleasantly surprised with PHP 5's DOM extension, which is light-years beyond what you're using in PHP 4.
4. An Embedded Database with SQLite
While MySQL is greater than ever, it's actually "too much database" for some jobs. SQLite is an embedded database library that lets you store and query data using an SQL interface without the overhead of installing and running a separate database application.
When your application needs a server-side storage mechanism but you can't rely upon the presence of a specific database, turn to SQLite. It correctly handles locking and concurrent accesses, the two big headaches with homebrewed flat files.
PHP 5 bundles SQLite, providing developers with a database that's guaranteed to work on all PHP 5 installations. Despite the name, SQLite is nowhere close to a "lite" database. It supports:
1-Transactions
2-Subqueries
3-Triggers
4-And many other advanced database features
You can even write user-defined functions in PHP and call them from within SQLite. This is by far and away the coolest feature available in any PHP database extension.
5. Cleaner Error Handling with Exceptions
PHP 5 offers a completely different model of error checking than what's available in PHP 4. It's called exception handling. With exceptions, you're freed from the necessity of checking the return value of every function. Instead, you can separate programming logic from error handling and place them in adjoining blocks of code.
Exceptions are commonly found in object-oriented languages such as Java and C++. When used judiciously, they streamline code, but when used willy-nilly, they create spaghetti code.
Right now, only a few PHP extensions use exceptions, but they're slowly being phased in. However, they're available today for any PHP code you write.
6. A First-Class SOAP Implementation
SOAP is a key component of the fast-growing web services field. This extension lets developers create SOAP clients with or without a Web Services Description Language (WSDL) file, and also implement SOAP servers in PHP.
PHP 4's SOAP support is only fair. While there are a few SOAP packages, the most mature ones are written in PHP instead of C. Therefore, they are slow, and you have to download and install them yourself.
With PHP 5, there's finally a usable SOAP extension written in C. Currently, this extension implements most, but not all, of SOAP 1.2. This is a significant improvement over previous C extension, and future pieces will be added in time.
Particularly in comparison with .NET and Java, PHP's SOAP support has always lagged behind the curve. Whether you love or hate SOAP, PHP needs to offer a first-class SOAP extension, and I'm excited to finally see some momentum in this direction.
7. Iterators
Iterators are a completely new PHP 5 feature. They allow you to use a for-each loop to cycle through different types of data: directory listings, database results, and even XML documents. SPL -- Standard PHP Library -- is a collection of iterators that provide this functionality and also filter, limit, cache, and otherwise modify iterator results.
Iterators are an incredibly handy way to abstract away messy details from your code.
For example, the DirectoryIterator turns directory iteration from this:
$dir = opendir($path);
while (false !== ($file = readdir($dir))) {
print "$file\n";
}
closedir($dir);
Into this:
foreach (new DirectoryIterator($path) as $file) {
print "$file\n";
}
There are no directory handles to mess about with, nor ugly conditions to check inside a while loop.
These are my top seven favorite features of PHP 5, but they're by no means the only ones. Besides what I've already highlighted, PHP 5 also offers: