1.2 What's New in PHP 5?

Better object-oriented features aren't the only new features of PHP 5. Many extensions have been rewritten to take advantage of PHP 5's new capabilities, and many new extensions have been added to the distribution.

1.2.1 MySQL

The MySQL database is PHP's partner in crime. Many developers power their web sites with MySQL, yet PHP's original MySQL extension dates back to the days of PHP/FI. It's showing its age.

In retrospect, some design decisions for the MySQL extension weren't the best solution 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 the MySQL Improved extension, MySQLi offers prepared statements, bound parameters, and SSL connections. It even takes advantage of PHP 5's new object-oriented support to provide an OO interface to MySQL. This extension is covered in Chapter 3.

1.2.2 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. It's the topic of Chapter 4.

PHP 5 bundles SQLite, providing developers with a database that's guaranteed to work on all PHP 5 installations. Despite the name, SQLite is a nowhere close to a "lite" database. It supports transactions, subqueries, triggers, and many other advanced database features.

Like MySQLi, SQLite also comes with dual procedural and OO interfaces.

1.2.3 XML

XML is a key part of web development, so PHP 5 offers a full selection of new XML tools. A major goal of XML in PHP 5 is interoperability among each of the different XML extensions, making them a unified unit instead of individual agents.

The new Document Object Model (DOM) extension is leaps and bounds better than PHP 4's experimental version. It also uses new PHP 5 features to comply with the DOM specification, fulfilling the goal of DOM as a language-neutral API for XML.

There's also a new Extensible Stylesheet Language Transformations (XSLT) extension that operates on XML documents and DOM objects. You can transform DOM objects using XSLT and receive translated documents from XSLT. You can even pass XML nodes back and forth between XSLT and PHP from within a stylesheet.

Perhaps the most innovative part of PHP 5 is the SimpleXML extension. This lightweight interface to XML lets you easily iterate through XML documents without the overhead of DOM or the oddness of XSLT. It's perfect for documents where you know the specification and want to quickly extract data.

Chapter 5 covers all these topics, plus the original Simple API for XML (SAX) extension and an XPath extension, used for querying XML documents. If you're new to XML or want a refresher on some of its more difficult parts, such as namespaces, be sure to read Appendix A.

1.2.4 Iterators and SPL

Iterators are a completely new PHP 5 feature and the topic of Chapter 6. They allow you to use a foreach loop to cycle through different types of data: directory listings, database results, and even XML documents.

SPL?the 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.

1.2.5 Error Handling and Debugging

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.

Chapter 7 covers exceptions.

1.2.6 Streams, Filters, and Wrappers

Streams allow you to place a file interface on reading and writing data using protocol-specific objects known as wrappers. Streams also let you modify the data flowing through them by attaching filters.

First introduced in PHP 4.3, streams are an underutilized part of PHP. PHP 5 expands upon the existing streams infrastructure to allow more extensive wrappers, and to let you create filters directly in PHP instead of in C.

Chapter 8 demonstrates how to create a shared memory wrapper and two streams, one for encoding HTML entities and another for decoding them.

1.2.7 Other Extensions

Chapter 9 covers three new pieces of PHP 5: SOAP, Tidy, and the Reflection classes.

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.

The Tidy extension makes it easy to ensure that your output is valid HTML and XHTML. Its smart parser brings even the most unruly of files into compliance with the latest W3C specifications.

Nothing less than complete code introspection is the goal of the Reflection classes. This set of classes lets you examine classes, methods, parameters, and more, to discover object attributes.

It is now simple and easy to create PHP class browsers, debuggers, and other tools that rely on gathering details about objects and functions.

1.2.8 PHP 5 in Action

It's one thing to see each of the individual parts of PHP 5 in isolation; it's another to see them in harmony. Chapter 10 provides you with a small address book application that combines the new PHP 5 features into a complete unit.

This chapter pulls together the concepts introduced in the rest of the book and provides you with an example that shows exactly why application development in PHP 5 is so great. Even in a short program, you can take advantage of SQLite, DOM, SimpleXML, iterators, abstract classes, exceptions, and property overloading to create an application that's flexible in many dimensions, yet easy to write and understand.

This address book lets you add contact information and search stored records using both a web and command-line interface. The output is completely separated from application logic, and the program uses a simple series of template classes to control its formatting.

1.2.9 Everything Else

PHP 5 is such a major update that it's impossible to cover it completely in only 10 chapters. Additionally, some features are so minor, they're only worth mentioning briefly.

Appendix B contains all the small changes and fixes that aren't mentioned in the chapters. It's definitely worth a read because, in many ways, it's easiest to be tripped up by minor changes. You know you're on new ground with the mysqli extension, but, unless you read Appendix B, you probably would not know that you can now pass optional parameters by reference, or that strrpos( ) behaves slightly differently than before, or even that the command-line version of PHP has the ability to execute code on every line of input.