PHP 5 Trends in 2004

Featuring: PHP 5 Beta 4

 PHP 5 beta 4 almost snuck out the door so its time to take a good look at what's in store for developers. If all goes well PHP 5 should have a final release by the end of March early April 2004. Yet at first this developer is approaching the upgrade with wariness. It is almost as if there is a bit of avoidance psychology here. Having been through the agonies of Microsoft VB6 to VB.NET conversions, and the still lingering Perl 5=>6 excursion , and the very recent ActionScript 1 to 2 "little surprises"; your reviewer has seen a lot of work being piled on to developers in terms of learning new constructs and converting programs from the old to new language standards - and all of which are being done in the name of "greater OOPs-I-did-it-again purity". In sum, this beleaguered and belabored-with-keeping-up-with-it-all developer was not anxious to see what the gods of Zend language design had unleashed upon unsuspecting and innocent developers.

And given that the PHP 5 upgrade was also for OOP-Object Oriented programming purity, the prospects were ... too close to a possible perversion. Imagine my pleasant surprise when I discovered the new PHP 5 is chock full of useful new functionality and well designed extensions. And the new stuff does not break the old except in some clear cases of object usage. However the new SimpleXML and libxml2 based functions are so good they may very rapidly displace old coding practices in the XML arena. And the ability of PHP 5 to act as a command line utility right out of the download is such a godzend - you can clearly see that this preliminary reviewer is delighted with the PHP 5 upgrade path to date. Take a look at what the PHP 5 beta has to offer and decide for yourself.

PHP Status

In Internet development circles, the acronym LAMP stands for the most popular Web server OS, Web server system, Web database and Web scripting language. LAMP is short for Linux-Apache-MySQL-???? Perl or PHP. If you go to the TIOBE Index which ranks software by the frequency/density of its references on the Web then that choice should be Perl which is in position four outranked only by Java, C, and C++. However, if you go to the Netcraft Surveys you will see the most popular language on Apache Servers by number of modules used appears to be PHP. Both languages are scripted. Both languages run on Linux and Windows plus other server platforms. They resemble each other because PHP clearly is influenced by both its scripting predecessors - Perl and JavaScript. Both languages are in for an Object-Oriented refitting. And it appears that PHP will be out of the gate with its final OO syntax and refinements before Perl 6.

But perhaps the most interesting development from a usage point of view in PHP 5 is that the program can be used in the CLI-Command Line Interpreter mode right off the download and compile(or download and install if using the executable binary). Before users had to make changes to php.ini file and/or switch executables and .DLLs in Windows. Regardless of platform, getting into command line mode was a nuisance. No Longer - as can be seen from the figure below.

The screenshot shows entering the command line mode and getting its help options. There are two main ways to run PHP in command line mode:
1 - Interpret commands from a file -   php phptest.php
2 - Run interactively from PHP -   php -a
In the latter case users may want to change the php.ini command file as follows:
max_execution_time = 30000   <= this sets the max execution time to about 8 hours
implicit_flush = On     <= any output statement immediately prints at block closing.
So echo "Hello World" prints immediately; but
for(var $ii=1; $ii<=10; $ii++){
  echo "this is the number ".$ii."\n";

waits until the block is closed with the final "}" before printing out ten lines.
Agile modelers and extreme programming teams will be ecstatic. This means that PHP becomes a wonderful immediate test bed for trying out code, just like Perl and Java are already. Want to try out all those PHP 5 new object definition examples shown on the Zend website ? Just copy and paste into your favorite editor, save it as a file on disk (it doesn't have to have the .php suffix, but it helps in refinding later) and run as shown in the example below:

So for the first time PHP will be able to compete on a more even footing with Perl on Linux and PC desktop, being more command line adept like Perl but also serving its Web users as well. But obviously PHP 5 is more than having a command line mode. And in fact the additions have an interesting logic to them.

The primary aim of this version of PHP was to fix the OO infrastructure from a somewhat kludgy runtime implementation and at the same time to broaden the OO infrastructure while adding modern try/catch exception handling constructs. But as usual the Zend/PHP folk have added a whole bunch of extra goodies to the PHP function libraries not the least of which are libxml2 support, a new SimpleXML syntax model which is going to be quite influential, and SQLite support which adds a new very lite, but complete SQL database to the mi x. And we have not even mentioned some of the other goodies. So without further to do lets see what's new in PHP 5.

What's New in PHP 5

One of the advantages of scripting languages like PHP is that they are easy to code in and test because o their fast, interpreted turnaround. PHP with its new out-of-the-box interpreted CLI-Command Line Interface reinforces that ease of use. But the scheduled fixes to the OO mechanism had some PHPers worried that PHP would become so much more complex to code in. Not to worry. the OOP extensions do not impose an inheritance hierarchy on coders like Java and really do put PHP on a much more solid OOP footing. Here are some of the notable OOP improvements.
-OOP objects are assigned by handle not object structure. _clone() copies all of object see example code above. These changes will speed up PHP OO processing;
-new protected, private, public properties for variables and methods insure objects have requisite encapsulation protection;
-static property means class wide properties and methods are available;
-final and abstract properties allow further refinement of coding templates: abstract defines a minimum implementation; final disallows any further inheritance;
-interface works just like in Java requiring specific methods to be implemented;
-__call() handler allows for object methods to be overloaded with variations;
-__autoload() includes classes programmatically.
This is just a cursory overview of the new OO implementation in PHP5. Read more about it with detailed examples at SitePoint and Zend. However, having done some class coding and worked through the exercises, PHP5 appears to have struck a neat balance between OO purity and pragmatic functionality. For example, the __call() overloading method would surely not make the cut in Sun's Java but it adds discreet flexibility because now users can add variations on methods - say _call(paint, circle) but also __call(paint, circle, texture). One problem, through beta 2 some of the OO code could be flaky, bringing down the PHP interpreter. In PHP beta 3 and beta 4 there has been a notable improvement in stability; but also at the cost of withdrawal of the new and promising namespace code.

The next area where PHP has generally shored up its processing capabilities is in error handling. This is where the new OO functionality comes into play. First, PHP 5 supports three new keywords try, catch, throws which works just like in Java or the languages copying from Java like C# and Visual Basic. This upgrade puts error handling in PHP on a systematic basis so that coders are less likely to see all sorts of different error handling schemes and routines. Also, because exceptions are OO based, users can more readily customize exception handling routines by inheriting from the standard exceptions to write derivative exception classes that explicitly meet their needs while staying within a familiar exception handling framework. The downside to exception handling is that now unhandled exceptions are fatal - they cause PHP's runtime to come to a halt. Partially, this reflects the status quo; but there is a wider range of exceptions that must be handled or else.

Some Really Good Goodies

The bulk of the improvements to PHP were targeted to put it on a better standard for Object Oriented programming and exception handling. The proposed changes in PHP 5 appear to be well directed to achieve these ends. However there are some goodies in this update too. First there are the performance improvements. The new OO code runs faster, so does the new onboard database, SQLite, in single-user operations. Finally, a new memory manager has been added which streamlines memory handling in multi-threaded tasks. We have yet to do benchmarks as we await the final code. However, in standalone, single user tests we have seen the new SQLite standard database outperform MySQL and PosgreSQL; so the runtime performance indicators are positive.

But there can be no doubt one of the real goodies of the new PHP 5 is the adoption of the fast libxml2 code to support XML and XSLT parsing plus the adoption of SimpleXML. The latter is a brilliant simplification. After defining the root node, SimpleXML parses the XML into a tree in which each XML tag is just an object property whose contents are accessible in the normal PHP way. A bit of sample code will give the flavor:
  $s = simplexml_load_file($argv[1]);
//XML element text is picked up as if object properties: $s["version"]
  echo "Version=".$s["version"];
//Or XML tag element names are just pointed to from the root XML object: $s->item
//Then a foreach statement iterates through all the XML elements
  foreach ($lev as $iteminfo) {
    print "title= ".$iteminfo->title ."\n";
    print "link= ".$iteminfo->link ."\n";
    print "pubDate= " . $iteminfo->pubdate . "\n";
    print "------------------\n";

The XML Input File looks like this:
<?xml version="1.0" ?>
< chan>
   <title>States want Microsoft licensing improved </title>
   <pubDate>Fri, 16 Jan 2004 17:45:00 -0800</pubDate>
   <title>MIT Spam Conference looks beyond JBS filters</title>
   <pubDate>Fri, 16 Jan 2004 16:35:14 -0800</pubDate>
< /chan>

And the output from the following command line: php xml2.php inews.xml  is-
Content-type: text/html
X-Powered-By: PHP/5.0.0b3

title=States want Microsoft licensing improved
pubDate=Fri, 16 Jan 2004 17:45:00 -0800
title=MIT Spam Conference looks beyond JBS filters
pubDate=Fri, 16 Jan 2004 16:35:14 -0800

We found in PHP beta3 that SimpleXML routines tended to flounder on complex XML such as XSD schema files or complex XML file formats with namespaces, etc. Beta 4 promises improvements which we shall report on at a future date. Nonetheless, this simplification in XML parsing really throws down the gauntlet to other developer tools to make improvements in their languages that match this for the ever more important XML processing that is confronting developers everywhere.

And there are still more goodies. One that this developer really likes is the inclusion of SQLite with PHP 5. SQLite implements more of the SQL 92 standard than MySQL including triggers, subselects, and most joins. However, SQLite is exactly that - a single file database with very simple locking mechanisms (all inserts, updates, deletes lock the entire database), one level commit and rollback procedures, no smart caching, and no user partitioned files or tablespaces. Buts this simplicity is not misplaced in the world of data processing. For single user or read-only databases, this is a splendid vehicle because its SQL is quite robust and its performance is very reliable and very fast. So think of SQLite as solving a whole range of persistence problems associated with your systems that flat files quickly run out of steam for and XML is too verbose and/or does not have the querying finesse to handle. When requirements are multi-user or heavy transaction switch to the appropriate MySQL, Oracle, PostgreSQL, or other heavyweight database. Use the free ADO.lib drivers for SQLite and that switch is made even simpler.

And there are even more goodies in PHP 5. The addition of a Reflection API and the instanceof keyword will be welcomed by MDA developers who want to use models to drive the development and generation of basic IT architectures and the resulting backbone code for major middleware and support systems. Lots of new coding tools will have this type of wizard or generation component that can also take advantage of a more robust Object model in PHP 5. In addition there are simplifications to streams and basic transport mechanisms including the ability to access low-level socket operations on streams. Finally, there are a number of small improvements in a number of library functions - some are bug fixes and other are simple extensions. The result is a better PHP environ to develop with.


Lets not be coy. All in all we think that this upgrade to PHP is so good that it will give PHP a huge groundswell of increased popularity. The reason is simple. You can still hack easily in PHP 5; but now you can also adopt design rigor where required to add highly portable functionality through OO classes. Performance is improved and with Zend's new WinEnabler FastCGI, Windows PHP code runs as fast as and as stable as on Linux. It also helps that out of the box PHP is command line usable. This will create a secondary community of cross platform PHP apps that do all sorts of useful system administrative chores that can be run live or from a browser. The SimpleXML and incorporation of libxml2 could not be timed more brilliantly. With the succession of OpenOffice and Office 2003 using XML as storage format, the emergence of native XML databases, and the more frequent use of Web Services and the imminent release of Macromedia Flex using MXML and Adobe's XML-based XDP plus the wild popularity of RSS-feeds and Weblogs - being able to handle XML as straight forwardly as in in SimpleXML and with the speed of libxml2 is going to be very attractive to a lot of developers. Also, if used appropriately (think single user and/or primarily read-only databases) SQLite is also going to make providing of persistence aids such as saving a users different desktop workspaces much more practical. Finally, there are the blitz of new PHP5 object libraries that are sure to surface in the next year or two. PIMP, a photo processing library par un Francais qui sait Anglais tres bien telque il choisit le nom de "PIMP" avec un sourire mechant. PIMP is impressive as its methods can be called either in classic PHP functional form or using OO syntax. Those routines allow users to draw 2-1000 times faster than GD; do sophisticated image blends and color adjustments; do vector and font graphics with antialiasing; do filtering with a choice of dozens of effects and the ability to add plugin libraries. With this class library expect to see a flood of smart photo albums that allow end users to correct, annotate , and make "salescards" or calendars for online email or offline printing. Expect to see mapmakers unite maps with images of local business or sites to see. Expect to see online sales catalogs customized to the size and styling of product images and info pitch that each customer wants

. In sum, without even trying hard, PHP 5 beta 4 already suggest so many possibilities only a huge performance drop and/or buggy gaffe could do in PHP 5 now. The lack of an IDE with drag and drop visual layout of forms and reports is another possible factor keeping PHP 5 from being one of the biggest developer debuts in a year which will be filled with some major coming out parties.


Top of Page  Tutorials Home  PHP References and Links