Andi Gutmans

Subscribe to Andi Gutmans: eMailAlertsEmail Alerts
Get Andi Gutmans: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Related Topics: Apache Web Server Journal, PHP Developer's Journal

Apache Web Server: Article

PHP 5: Evolutionary or Revolutionary?

A look at the history and future of PHP

With PHP 5 around the corner, many are asking, "Is PHP evolutionary or revolutionary?" This article tries to answer this question.

Many readers might have already read about PHP 5 or heard of its numerous new features. As the current PHP 5 code base is already extremely stable, some production development with PHP 5 has already begun.

One question that has been heard numerous times is whether this new version is evolutionary or revolutionary. Obviously, this is a matter of perception, and different people have different views. This article tries to answer this question by looking back at previous changes in PHP's major versions and by examining PHP 5's changes, which should allow you to judge for yourself how the new features compare against the major new features introduced in previous versions of PHP. This article will focus on PHP 5's new features as they are the biggest interest.

PHP/FI 2 to PHP 3

We started our involvement in PHP's history when we rewrote the core of PHP. The most notable news of PHP 3 was the rewrite of the scripting language itself. Not only did this include a much more powerful language syntax, as opposed to previous versions, but probably the main advantage of the new scripting engine was an API we exposed which allowed to easily write new add-on extensions for PHP. This API led many developers around the world to contribute dozens of new PHP extensions. These extensions gave PHP developers access to many technologies required to implement Web applications, from basic functions required daily by PHP developers to support of numerous databases, regular expressions, image creation, XML support, and lots more.

Arguably, the most notable change in PHP's history is probably the extension API. It was so successful that even today, with PHP 5, the API still very much resembles that of PHP 3. Also, one of PHP 3's most valuable additions were the object-oriented features, which although simple allowed people to write impressive object-oriented frameworks for PHP such as the then popular phplib.

In addition, PHP 3 was the first version of PHP to reach an install base of hundreds of thousands and later on, over a million domain.

PHP 3 to PHP 4

PHP 4 was sparked by yet another rewrite of the scripting engine, called the Zend Engine. Unlike PHP 3, this was a "real" interpreter that reparsed the scripts as it was executing them. This new incarnation of the scripting language moved to a compile-first execution-later architecture. What this means is that the Zend Engine first compiled scripts into intermediate code (or bytecode) and would then execute this intermediate code, without having to reparse the scripts over-and-over during execution. Not only did this new engine bring an enormous performance increase but it also added to the stability and the maintainability of the scripting engine. Other notable additions to PHP 4 included a Web server abstraction layer, allowing PHP to plug in to any Web server, as opposed to PHP 3, which only worked natively with the Apache Web server (and everywhere else as a CGI binary, which led to poor performance). Also, a new native HTTP sessions extension, although available in third-party libraries such as phplib, made session management dummy proof and highly accessible to all PHP developers. In addition, the new PHP 4 architecture allowed for writing plug-ins to the engine including debuggers, which drastically improved the development and debugging abilities with PHP.

PHP 4 to PHP 5

The initial reasoning

According to Netcraft, today PHP is installed on over 15 million domains. No doubt, it is very hard for a new version of PHP to better this amazing success. You might think that PHP has come to completeness; however, after talking to a huge amount of PHP developers, especially those working on medium to large Web applications, it became clear to us that PHP 4 was not ready for this task. One of the main reasons is that the larger an application gets, the more likely it is that developers will want to use object-oriented features. This is probably due to such people preferring the object-oriented development process and methodologies, which often improve the maintainability and reusability of the application's components (although good functional design is also possible).

When object-oriented capabilities were added during a sleepless night back in PHP 3, objects were added as native types. At a much later stage it became apparent that this was a mistake. Objects behaving as native types meant that unless special precautions were taken, objects were always copied during assignments and parameter passing. Unlike other languages such as Java, this meant that there would be all sorts of weird behavior going on. For example, you'd pass an object to a function, which would then change one of its properties, and after the function would return, the object would remain unchanged (see Listing 1).

Most people would expect this script to print out "Dog". However, in both PHP 3 and PHP 4 this would print out "Cat". As mentioned previously, the reason is that $obj is passed by-value to the changeName() function, which duplicates it and modifies a duplicate. After the function returns, the original $obj remains unchanged.

Not only was this odd behavior hard to debug (especially as most developers weren't aware of this deficiency), it also had an affect on performance (many behind-the-scenes duplications) that prevented it from supporting additional object-oriented features such as allowing de-referencing of objects returned by functions, e.g. $obj->method1()->method2(), which is not only syntactic sugar but also very useful when interfacing with external object models such as COM and Java. Also, destructors would have been impossible to implement because it wasn't clear if/when the developer really meant for this duplication to happen.

Those who wanted to work around the problem of excessive duplications had to always pass, return, and assign objects by reference. Very soon, applications contained dozens of "&" signs that would tell the engine to treat objects as references. This not only led to the code being quite ugly, but developers (the ones who knew of this problem) would also forget them in certain places.

The Zend Engine II

When we decided to improve the Zend Engine's object-oriented feature, we wrote an initial feature overview of what we thought should be addressed in a new version. You can see this document in the history section at www.zend.com/php5/zend-engine2.php. Once published, discussions began on the PHP mailing lists. The end result was in many cases quite different than our original proposal; however, the main change. which solves the deficiency of objects being native types, remained the same. In the Zend Engine II, objects are now handles, meaning that when you assign objects, pass, or return them to and from functions, you always end up pointing at the same object. This resembles Java's object handle paradigm or C++ pointers where you access objects via an identifying value and not as a native type. Additional changes in the Zend Engine II include:
  1. Property and method access modifiers such as public, private, and protected.
  2. Support for de-referencing method return values such as the above example $obj->method1()->method2().
  3. Interfaces and abstract classes.
  4. Exception handling a la try/throw/catch.
  5. Destructors.
  6. Static methods and members.
  7. New "instanceof" operator.
  8. Ability to overload the object-oriented syntax from extensions allowing strong integration with other object models such as COM and Java.
  9. And lots more… More information available at www.zend.com/php5/whats-new.php.
Although the PHP 5 scripting language has many more features, it is not yet-another-rewrite of the scripting engine, but builds very strongly on PHP 4's Zend Engine architecture. The reason is that the PHP 4 scripting engine is extremely mature and its stability and performance has proven itself over the past few years.

Greatly Improved XML Support

General XML

In our opinion, immediately following Zend Engine II, PHP 5's support for XML technologies has made incredible advancements. In PHP 4, XML support was far from being satisfactory. The primary reason was that each XML extension, such as SAX, DOM and XSL Translation, were implemented on top of different XML libraries. SAX was using the relatively old Expat library, DOM was using the powerful Gnome project's libxml2 library, although the PHP extension itself did not completely conform to the DOM standard, and XSLT support was implemented using the Sablotron library.

In PHP 5, most of the XML extensions were rewritten to use the excellent Gnome project's XML and XSLT libraries (www.xmlsoft.org). Not only do these libraries bring increased performance, stability, and standard compliance to PHP, but they also allow the various XML extensions to interoperate with each other when required.

SimpleXML

It is very likely that the most significant XML feature in PHP 5, is the new SimpleXML extension, which may also be nicknamed as "XML for Dummies." This XML extension allows you to manipulate XML files as if they were PHP objects, meaning that any PHP developer can easily use XML without having to know about the SAX or DOM standards. For example:

<news_sites>
<site>
   <name>CNN.com</name>
    <url>http://cnn.com</url>
</site>
<site>
     <name>FOX News</name>
     <url>http://www.foxnews.com</url>
</site>
</news_sites>

The following script reads this XML and creates the relevant HTML anchors:


<?php

$sites = simplexml_load_file('sites.xml');
foreach ($sites->site as $site) {
     print "<a href='$site->name'>$site->url</a>\n";
}
?>
The output of this script would be:

<a href='CNN.com'>http://cnn.com</a>
<a href='FOX News'>http://www.foxnews.com</a>

This example surely speaks for itself. No complicated SAX callbacks, no DOM tree traversing - you just access the XML file as a regular PHP object. Using XML has never been easier. Now if you do happen to have to do some fine-tuning using DOM functionality which can't be accessed using SimpleXML, as SimpleXML is also built on top of the Gnome XML library, you can actually convert your SimpleXML object into a DOM tree and back (using dom_import_simplexml() and simplexml_import_dom()), without very much run-time penalty, giving you the best of both worlds. Now if that isn't impressive then what is?

SOAP and Web Services

Anyone familiar with PHP 4 knows that SOAP support was missing from the standard PHP distribution. Developers looking for ways to consume Web services needed to find other alternatives. These alternatives include PEAR SOAP classes (http://pear.php.net – a repository for PHP classes) and other C extension solutions such as a preliminary SOAP extension in PHP's Pecl project (a C extension repository). However, it was clear that sooner or later good SOAP support needed to be implemented in the default PHP distribution, especially due to its increased adoption in medium to large organizations.

Although developed very late in PHP 5's release cycle, the SOAP extension, also built on top of the excellent libxml2, was included in PHP's default distribution. This extension supports both SOAP client and server support and takes advantage of the new possibilities for overloading PHP's object oriented syntax from C extensions.

The following example shows how easy it is to call a SOAP service using the new functionality. It retrieves the value of Microsoft's stock quote.


<?php

$client = new  
SoapClient
("http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl");

print($client->getQuote("MSFT"));

?>

The new extension not only has good performance due to it being implemented in C but it also caches WSDL files so that retrieval is not performed on every SOAP call.

Improved Database Support

No new major version of PHP is complete without improving PHP's database support. PHP 5 has two major improvements on this front, the new MySQLi and SQLite extensions.

MySQLi

MySQL AB did not author the original MySQL extensions but as the majority of PHP users today are using MySQL, it has probably become strategic for the MySQL company to make sure that PHP's support is up to par with their product. This becomes especially important with MySQL 4.1 and 5.0 where many new features have been added, such as an improved client-server binary protocol including encryption, and most notably, prepared statements. For this reason, MySQL AB has written a new MySQL extension for PHP 5, called MySQLi (MySQL Improved) which allows PHP users to take advantage of all of the new goodies.

Anyone familiar with advanced database features knows the importance of prepared statements. Not only does it potentially improve performance for queries that are re-ran with different parameters, but they also provide the developer with increased security as no parameter escaping needs to take place due to the postponed binding of query parameters.

Also, the MySQLi extension takes advantage of the new Zend Engine II features, providing developers with an additional object-oriented API (for people who prefer working with object-oriented interfaces) and by supporting the scripting engine's ability to let extensions overload foreach() iteration, thus making it even easier to iterate over result sets.

SQLite

SQLite (www.sqlite.org) is an embedded database, which allows use of SQL without having access to a full-blown database server. Contrary to the implications of its name, the SQLite database has many features, such as support for transactions, sub-selects, and other advanced database features. Although it might not be suitable for all uses, especially on systems that deal with heavy and frequent database updates, it is very suitable for most other projects, such as applications with a high frequency of database reads and less frequent writes, or as a powerful replacement for flat files in your applications.

This lightweight database is also extremely useful for PHP developers who don't have database server access on their hosting machine, giving them the opportunity to use advanced SQL features.

Conclusion

This article covers only some of the new language features, extensions, and PHP changes. Going back to the title of this article, it is up to you to decide if PHP 5 is evolutionary or revolutionary. Our opinion is that if you look at PHP 5 from a technical perspective, it is evolutionary. This is not a complete rewrite of the scripting engine and the infrastructure is similar to the one in PHP 4. However, when looking at the vast functionality improvements and new features that are part of PHP 5, it seems to be just as revolutionary as any previous version of PHP.

One thing is clear: with the rich functionality PHP 5 provides while still leaning on the proven PHP infrastructure, PHP will remain the most popular Web scripting language in the world. This new version only increases the challenge for its competitors to catch up.

Enjoy!

Resources

  • PHP Web site: www.php.net
  • Zend.com Web site: www.zend.com
  • Zend.com PHP 5 Information Center: www.zend.com/php5

    Listing 1

    <?php
    class MyClass {
    var $name = "Cat";
    }
    function changeName($obj, $str)
    {
    $obj->name = $str;
    }
    $obj = new MyClass();
    changeName($obj, "Dog");
    print $obj->name;
    ?>
  • More Stories By Zeev Suraski

    Zeev Suraski is one of the creators of PHP and the Zend Engine, on which all PHP sites and applications are run. He is also the author of some of the most popular PHP modules, such as the MySQL and PostgreSQL extensions. Recently, Zeev has been working on Zend Engine II – the core of the upcoming PHP 5 release.

    Comments (0)

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.