Object-Oriented Features New to PHP 5
In PHP 5 all Extensible Markup Language (XML) support is provided by the libxml2 XML toolkit (www.xmlsoft.org). The underlying code for the Simple API for XML (SAX) and for the Document Object Model (DOM) has been rewritten, and DOM support has been brought in line with the standard defined by the World Wide Web Consortium.
Unified treatment of XML under libxml2 makes for a more efficient and easily maintained implementation. This is particularly important because support for XML under PHP 4 is weak, and web services present many problems that require an OO approach.
Under PHP 4, creating a SOAP client and reading an RSS feed are challenging programming tasks that require creating your own classes or making use of external classes such as NuSOAP (sourceforge.net/projects/nusoap). There's no such need in PHP 5. In Chapter 12, you'll see just how easy these tasks are using the built-in SOAPClient class and SimpleXMLElement. Again it's the improved object model that makes this possible.
The reflection classes included in PHP 5 provide ways to introspect objects and reverse engineer code. The average web developer might be tempted to ignore these classes, but Chapter 14 shows how useful they are for automating a task that most developers approach with little enthusiasm: the creation of documentation.
In addition to built-in classes, PHP 5 also offers built-in interfaces. Iterator is the most important, as a number of classes and interfaces are derived from this interface. I'll show you how to use Iterator in Chapter 10.
Backward compatibility may be an issue if your code already uses objects. PHP 5 introduces a number of new "magic" methods. Magic methods begin with a double underscore, and this requires changing any user-defined methods or functions that use this naming convention. All of these methods will be discussed, particularly in Chapter 13. The most important ones relate to how objects are created and destroyed. The PHP 4 style of object creation is still supported, but you are encouraged to use the new magic method approach.
PHP 5 deprecates some existing object-related functions. For example,is_a has been replaced by a new operator, instanceof (see Chapter 14). This particular change won't affect how your code runs under PHP 5. If you use a deprecated function, you'll see a warning if the error-reporting level is set to E_STRICT (a useful technique for discovering where your code may need upgrading and discussed in more detail in Appendix A). In another example, the get_parent_class, get_class, and get_class_methods functions now return a case-sensitive result (though they don't require a case-sensitive parameter), so if you are using the returned result in a case-sensitive comparison you will have to make changes.
The preceding examples of changes are relatively minor and fairly easy to detect and upgrade. However, there is one change in particular that is of an entirely different magnitude.
The major change to PHP in version 5 relating to OOP is usually summed up by saying that objects are now passed by reference. This is true enough, but don't let this mask what's really at issue: a change in the way that the assignment operator works when used with objects.
Granted, the assignment operator is often invoked indirectly when an object is passed to a function or method, but objects are now passed by reference because of the implicit assignment. Prior to PHP 5, the default behavior was to assign objects by value and pass them to functions by value.
This is perfectly acceptable behavior for primitives, but it incurs far too much overhead with objects. Making a copy of a large object by passing it by value can put strains on memory and in most cases, all that's wanted is a reference to the original object rather than a copy. Changing the function of the assignment operator is a fairly significant change. In fact, the scripting engine that underlies PHP, the Zend engine, was entirely rewritten for PHP 5.
NOTE: In PHP 4 it's possible to pass objects by reference using the reference operator (&), and in fact it is good programming practice to do so. Needless to say, this use of the reference operator becomes entirely superfluous after upgrading to PHP 5. We'll discuss the implications of this change in Chapter 13, in the section "__clone" on page 116.
- The Tiny Internet Project, Part I
- Machine Learning with Python
- SUSECON 2016: Where Technology Reigns Supreme
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Free Today: September Issue of Linux Journal (Retail value: $5.99)
- Bitcoin on Amazon! Sort of...
- Securing the Programmer
- Android Browser Security--What You Haven't Been Told
- Epiq Solutions' Sidekiq M.2
Pick up any e-commerce web or mobile app today, and you’ll be holding a mashup of interconnected applications and services from a variety of different providers. For instance, when you connect to Amazon’s e-commerce app, cookies, tags and pixels that are monitored by solutions like Exact Target, BazaarVoice, Bing, Shopzilla, Liveramp and Google Tag Manager track every action you take. You’re presented with special offers and coupons based on your viewing and buying patterns. If you find something you want for your birthday, a third party manages your wish list, which you can share through multiple social- media outlets or email to a friend. When you select something to buy, you find yourself presented with similar items as kind suggestions. And when you finally check out, you’re offered the ability to pay with promo codes, gifts cards, PayPal or a variety of credit cards.Get the Guide