A Look at PHP 5, Part II

Tuesday Feb 17th 2004 by W. Jason Gilmore

The new OOP features are just a smattering of what's available in PHP 5. Take a look at the addition of three major features: The default packaging of the SQLite and SimpleXML libraries, and the addition of exception handling.

In the first article of this two-part series, we examined what is arguably the most significant feature of PHP 5; the significant advances to its object-oriented programming capabilities. Among other topics, I introduced the addition of the final, private, protected, abstract and static method scopes, as well as the new private and protected class member scopes. We also examined the new abstract class and interface features, before concluding the article with a look at the new unified constructors and destructor feature, in addition to an introduction to the ability to add class-specific constants. Yet despite all of this, the new OOP features are just a smattering of what's available in PHP 5. In this article, we'll examine several other cool new extensions and features available to this major release.

In particular, we'll take a look at the addition of three major features: The default packaging of the SQLite and SimpleXML libraries, and the addition of exception handling. Each in their own right, all three features stand to contribute significantly to your PHP programming repertoire.


SQLite (http://www.sqlite.org/) is a lightweight SQL database engine which offers a surprising number of advanced features. Although surprisingly small (for example, the Windows binary is 275kb in size), SQLite is largely SQL-92 compliant, supports ACID transactions, and is capable of working with databases up to 2 terabytes in size. Perhaps due both in part to the unbundling of the MySQL client library and the realization that the scope of many projects simply did not require the firepower provided by the traditional database servers, the decision was made to enable support for the SQLite extension by default.

To use PHP's SQLite extension, you'll need to download and install the package, available via the URL referenced in the previous paragraph. Once installed, you can create a new database via its command-line interface. This interface is quite similar to those available to MySQL and PostgreSQL. To create a new database, just pass its name as an argument to the sqlite command. Next, you can create a table and insert a few rows using commands again quite similar to MySQL and PostgreSQL. I'll demonstrate this process here:

%>sqlite library.db
sqlite>CREATE TABLE book (
...>title VARCHAR(55),
...>author VARCHAR(55));
sqlite>INSERT INTO books VALUES(NULL,"Practical Python","Magnus Hetland");
sqlite>INSERT INTO books VALUES(NULL,"MySQL","Michael Kofler");

Next, let's use PHP's SQLite library to extract this information from the newly created "library.db" database:

   $sqldb = sqlite_open("/www/sqlitedatabases/library.db");
   $results = sqlite_query($sqldb, "SELECT title,author FROM book");
   while (list($title, $author) = sqlite_fetch_array($results)) {
        echo "Title: $title (Author: $author) <br />";


Title: Practical Python (Author: Magnus Hetland) 
Title: MySQL (Author: Michael Kofler) 

For those of you with experience working with any PHP-compatible database, you'll find the above syntax to be quite familiar. Indeed, it matches almost exactly that used by PHP's MySQL library, with one significant difference; no authentication is required! This is because SQLite's permission framework is entirely dependent upon the permissions assigned to the file within which the database is stored. Therefore if the library.db file possesses adequate read-permissions, then it can be read via a PHP script. If it possesses adequate write-permissions, then SQLite insertion/update/deletion queries can be used, and so on. Indeed this convenient and effective permissions system works exceptionally well for users interested in keeping administration overhead to a minimum.

Prior to its packaging with PHP, I had no prior experience with SQLite, but have since used it for several internal projects. Fast, capable, and amazingly easy to configure and use, SQLite is definitely worth checking out. Learn more about SQLite via the PHP manual:


SimpleXML is one of the newer and resultingly, lesser-known extensions to work its way into the PHP 5 distribution, available by default as of the Beta 3 release. However, I suspect that its relative obscurity will soon change, given the extraordinarily convenient solution it offers to a problem that has long nagged developers: XML parsing. True to its name, SimpleXML works by loading an XML document into an object, allowing you to then dereference an XML node in a fashion quite similar to dereferencing an class member. Consider the following XML document:

Listing 1-1: A typical RSS feed (gilmore.xml)

<?xml version="1.0" encoding="utf-8" ?>
<rss version="0.91">
   <title>W. Jason Gilmore</title>
   <description>Ramblings of a computer maniac...</description>
   <title>Debugging NuSOAP</title>
   <description>Significantly reduce your PHP/NuSOAP debugging time!</description>
   <title>C# Patterns</title>
   <description>Found an excellent software patterns site this evening.</description>
You might recognize this as a typical RSS feed. While there are indeed numerous solutions available for parsing RSS feeds, there aren't too many that can do it as easily as SimpleXML:
   $xml = simplexml_load_file("gilmore.xml");
   echo $xml->channel[0]->title;
Returning: W. Jason Gilmore It doesn't get much easier than that, does it? Let's consider a somewhat more practical example, using the same RSS feed. This time, we'll convert the RSS entries to an XHTML-equivalent:
  $xml = simplexml_load_file("gilmore.xml");
  echo "<strong>".$xml->channel[0]->title."</strong><br />";
  foreach($xml->item as $item) {
    echo "<a href=\"$item->link\">$item->title</a><br />";
<strong>W. Jason Gilmore</strong><br />
<a href="http://www.wjgilmore.com/archives/000064.html">Debugging NuSOAP</a><br />
<a href="http://www.wjgilmore.com/archives/000058.html">C# Patterns</a><br />  

Learn more about this promising new extension via the PHP manual:

On an aside, in a previous tutorial I demonstrated how to create a custom RSS feed aggregator using PHP, MySQL and Magpie. The article can be found here.

Exception Handling

Many modern languages implement a common error detection and reporting methodology. One of the longstanding PHP annoyances has been the lack of such a feature. I'm happy to announce the PHP 5 resolves this problem, offering an "try-catch" exception model similar to that found in languages such as Python, Java and C#. Let's demonstrate this new feature, revising the previous SimpleXML example to include exception handling.

try {
   $xml = simplexml_load_file("gilmore.xml");
   if (!$xml) throw new Exception("Could not read RSS feed!");
   echo "<strong>".$xml->channel[0]->title."</strong><br />";
   foreach($xml->item as $item) {
      echo "<a href=\"$item->link\">$item->title</a><br />";
} catch (Exception $e) {
   echo "Exception: ".$e->getMessage()." on line ".$e->getLine();
As you can see, incorporating exception handling into our code can greatly increase its readability. Assuming that the simplexml_load_file() function successfully loads the XML file, the feed contents will be parsed and output accordingly, otherwise the following message will be output:
Exception: Could not read RSS feed! on line 13

Extending the default Exception handler

You can create your own custom exception handlers by extending the default Exception class. Let's revise the above example, this time using our own custom class:

class RSSException extends Exception {

   private $feed;
   protected $message;

   function __construct($feed, $message) {
      $this->feed = $feed;
	  $this->message = $message;
   function getFeed() {
      return $this->feed;

try {
   $feed = "gilmore.xml";
   $xml = simplexml_load_file($feed);
   if (!$xml) throw new RSSException($feed, "Could not read RSS feed!");
   echo "<strong>".$xml->channel[0]->title."</strong><br />";
   foreach($xml->item as $item) {
      echo "<a href=\"$item->link\">$item->title</a><br />";
} catch (RSSException $e) {
   echo "Exception: ".$e->getMessage().". Feed name: ".$e->getFeed();


Assuming there's a problem, the following output will occur:

Exception: Could not read RSS feed!. Feed name: gilmore.xml 

Note that there is currently no official documentation pertinent to PHP 5's new exception handling feature, although I suspect that it will soon be made available.


I welcome questions and comments! E-mail me at jason@wjgilmore.com. I'd also like to hear more about your experiences experimenting with PHP 5!

About the Author

W. Jason Gilmore is an Internet application developer for the Fisher College of Business. He's the author of the upcoming book, PHP 5 and MySQL: Novice to Pro, due out by Apress in 2004. His work has been featured within many of the computing industry's leading publications, including Linux Magazine, O'Reillynet, Devshed, Zend.com, and Webreview. Jason is also the author of A Programmer's Introduction to PHP 4.0 (453pp., Apress). Along with colleague Jon Shoberg, he's co-author of "Out in the Open," a monthly column published within Apress' dot-dot magazine.

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved