Section 1.3. General PHP Changes


1.3. General PHP Changes

1.3.1. XML and Web Services

Following the changes in the language, the XML updates in PHP 5 are probably the most significant and exciting. The enhanced XML functionality in PHP 5 puts it on par with other web technologies in some areas and overtakes them in others.

1.3.1.1 The Foundation

XML support in PHP 4 was implemented using a variety of underlying XML libraries. SAX support was implemented using the old Expat library, XSLT was implemented using the Sablotron library (or using libxml2 via the DOM extension), and DOM was implemented using the more powerful libxml2 library by the GNOME project.

Using a variety of libraries did not make PHP 4 excel when it came to XML support. Maintenance was poor, new XML standards were not always supported, performance was not as good as it could have been, and interoperability between the various XML extensions did not exist.

In PHP 5, all XML extensions have been rewritten to use the superb libxml2 XML toolkit (http://www.xmlsoft.org/). It is a feature-rich, highly maintained, and efficient implementation of the XML standards that brings cutting-edge XML technology to PHP.

All the afore-mentioned extensions (SAX, DOM, and XSLT) now use libxml2, including the new additional extensions SimpleXML and SOAP.

1.3.1.2 SAX

As previously mentioned, the new SAX implementation has switched from using Expat to libxml2. Although the new extension should be compatible, some small subtle differences might exist. Developers who still want to work with the Expat library can do so by configuring and building PHP accordingly (which is not recommended).

1.3.1.3 DOM

Although DOM support in PHP 4 was also based on the libxml2 library, it had bugs, memory leaks, and in many cases, the API was not W3C-compliant. The DOM extension went through a thorough facelift for PHP 5. Not only was the extension mostly rewritten, but now, it is also W3C-compliant. For example, function names now use studlyCaps as described by the W3C standard, which makes it easier to read general W3C documentation and implement what you have learned right away in PHP. In addition, the DOM extension now supports three kinds of schemas for XML validation: DTD, XML schema, and RelaxNG.

As a result of these changes, PHP 4 code using DOM will not always run in PHP 5. However, in most cases, adjusting the function names to the new standard will probably do the trick.

1.3.1.4 XSLT

In PHP 4, two extensions supported XSL Transformations: the Sablotron extension and the XSLT support in the DOM extension. PHP 5 features a new XSL extension and, as previously mentioned, it is based on the libxml2 extension. As in PHP 5, the XSL Transformation does not take the XSLT stylesheet as a parameter, but depends on the DOM extension to load it. The stylesheet can be cached in memory and may be applied to many documents, which saves execution time.

1.3.1.5 SimpleXML

When looking back in a year or two, it will be clear that SimpleXML revolutionized the way PHP developers work with XML files. Instead of having to deal with DOM oreven worseSAX, SimpleXML represents your XML file as a native PHP object. You can read, write, or iterate over your XML file with ease, accessing elements and attributes.

Consider the following XML file:

 <clients> <client>     <name>John Doe</name>     <account_number>87234838</account_number> </client> <client>     <name>Janet Smith</name>     <account_number>72384329</account_number> </client> </clients> 

The following code prints each client's name and account number:

 $clients = simplexml_load_file('clients.xml'); foreach ($clients->client as $client) {     print "$client->name has account number $client >account_number\n"; } 

It is obvious how simple SimpleXML really is.

In case you need to implement an advanced technique in your SimpleXML object that is not supported in this lightweight extension, you can convert it to a DOM tree by calling it dom_import_simplexml(), manipulate it in DOM, and convert it to SimpleXML using simplexml_import_dom().

Thanks to both extensions using the same underlying XML library, switching between them is now a reality.

1.3.1.6 SOAP

PHP 4 lacked official native SOAP support. The most commonly used SOAP implementation was PEARs, but because it was implemented entirely in PHP, it could not perform as well as a built-in C extension. Other available C extensions never reached stability and wide adoption and, therefore, were not included in the main PHP 5 distribution.

SOAP support in PHP 5 was completely rewritten as a C extension and, although it was only completed at a very late stage in the beta process, it was incorporated into the default distribution because of its thorough implementation of most of the SOAP standard.

The following calls SomeFunction() defined in a WSDL file:

 $client = new SoapClient("some.wsdl"); $client->SomeFunction($a, $b, $c); 

1.3.1.7 New MySQLi (MySQL Improved) Extension

For PHP 5, MySQL AB (http://www.mysql.com) has written a new MySQL extension that enables you to take full advantage of the new functionality in MySQL 4.1 and later. As opposed to the old MySQL extension, the new one gives you both a functional and an OO interface so that you can choose what you prefer. New features supported by this extension include prepared statements and variable binding, SSL and compressed connections, transaction control, replication support, and more.

1.3.1.8 SQLite Extension

Support for SQLite (http://www.sqlite.org) was first introduced in the PHP 4.3.x series. It is an embedded SQL library that does not require an SQL server, so it is suitable for applications that do not require the scalability of SQL servers or, if you deploy at an ISP that does not offer access to an SQL server. Contrary to what its name implies, SQLite has many features and supports transactions, sub-selects, views, and large database files. It is mentioned here as a PHP 5 feature because it was introduced so late in the PHP 4 series, and because it takes advantage of PHP 5 by providing an OO interface and supporting iterators.

1.3.1.9 Tidy Extension

PHP 5 includes support for the useful Tidy (http://tidy.sf.net/) library. It enables PHP developers to parse, diagnose, clean, and repair HTML documents. The Tidy extension supports both a functional and an OO interface, and its API uses the PHP 5 exception mechanism.

1.3.1.10 Perl Extension

Although not bundled in the default PHP 5 package, the Perl extension allows you to call Perl scripts, use Perl objects, and use other Perl functionality natively from within PHP. This new extension sits within the PECL (PHP Extension Community Library) repository at http://pecl.php.net/package/perl.



    PHP 5 Power Programming
    PHP 5 Power Programming
    ISBN: 013147149X
    EAN: 2147483647
    Year: 2003
    Pages: 240

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net