PHP 5’s new look

33

Author: Daniel Rubio

In July Zend Technologies announced the latest version of PHP, the widely used scripting language for Web programming. Version 5 incorporates a series of substantial enhancements over previous releases. Here’s an introduction to some of the new features.

The greatest change in PHP 5 comes with a complete redesign of its object model, and with it, a tighter integration to object-oriented (OO) paradigms. Previous versions’ usage of objects had one major drawback: Objects were not tightly aligned with the behavioural patterns observed in other object languages like Java or C++. While PHP offered a simpler approach, the disparity created a considerable chasm for those wanting to use PHP in a truly object-oriented manner — in the sense of what the industry perceives as object-oriented.

The first technically deficient issue surrounding the older object model was the fact that objects were passed around by value and not by reference. To a seasoned object developer, this created a series of pitfalls in the creation of object hierarchies and execution of operations, some of which could be resolved through cryptic syntax. To technical observers, this made PHP a bloated object technology, since it continuously created clones of objects being passed around on every invocation.

Now objects in PHP 5 are passed around by reference as a default behaviour, in much the same way other object-oriented languages do, making it easier to work with for those experienced in object languages, while preserving almost transparent backward compatibility with the older PHP object model.

At the core of this PHP shift is the second version of the Zend Engine, which powers this new behaviour of object references through special handles which are assigned to each object instance. The core PHP engine also incorporates more object-oriented features:

  • Interfaces allow for greater flexibility in the creation of class implementations and hierarchies.
  • Protected and private class methods and members provide enhanced capabilities for encapsulating class data, in the same way other OO languages do.
  • Static functions and variables ease the use of unique instance objects, or singletons, as they are dubbed in other OO languages.
  • Reflection allows the introspection of data within a same class.
  • Exceptions: In other PHP versions no provisions are made to trap class errors. PHP 5 adopts the natural OO syntax of defining catch sections to detect runtime exceptions.

Besides these enhancements, which are typically found in OO languages, there are many other new features similar to those found in Java or C++. However, the other central enhancement in PHP 5 is its tighter integration and faster execution when invoking components written in Java or .Net — possibly the two most adopted development platforms in today’s market — all of which comes as a result of the engine redesign.

PHP 5 also takes a revamped approach to another pervasive technology: XML. The newest PHP version uses the open source libxml2 library for core XML tasks like DOM and SAX parsing and XSL transformations, substituting the previous PHP versions’ approach of using a hodgepodge of libraries. SOAP support, as derivative of XML, was also rewritten as a built-in C extension in PHP 5, providing an enhancement over the widely used PEAR module (written in PHP) used to execute SOAP applications in older versions.

Another area which was also revised was the interaction of PHP with mainstream relational databases — a typical component used in Web applications. The PHP 5 distribution incorporates a new MySQL extension which allows applications to take advantage of the features included in MySQL 4.1 and later versions, such as prepared statements, SSL, and transaction control. Support for SQLite, an embedded SQL database, was also added to PHP 5, allowing for a lightweight alternative to a full-fledged database suite while leveraging the newest object-oriented features in PHP.

Other provisions included in the latest PHP release are the execution of Perl scripts from within application code; the use of Tidy, an open source library used to analyze and parse HTML code; and a new memory manager for enhanced RAM utilization.

Daniel Rubio is the principal consultant at Osmosis Latina, a firm specializing in enterprise software development, training and consulting based in Mexico.

Category:

  • PHP