Monday, March 31, 2008

Backward Compatibility

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. 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. This particular change won’t affect how your code runs under PHP 5. If you use a deprecated function, you will see a warning if the error-reporting level is set to E_STRICT. In another example, the get_parent_class, get_class, and get_class_methods functions now return a casesensitive result (though they do not require a case-sensitive parameter), so if you are using the returned result in a case-sensitive comparison you will have to make changes.

Pass By Reference

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 do not let this mask what is 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 is 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.

In PHP 4 it is 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.

Prognosis

The mere enumeration of the details of backward compatibility masks what can be a highly charged issue. Whenever you change an established language, there are competing interests. In many cases you are damned if you do and damned if you do not. For example, retaining inconsistent function naming conventions may be necessary to maintain backward compatibility, but you may also be criticized for this very lack of consistency.

Of course, breaking backward compatibility means that some existing code won’t function properly. In many circumstances it is not easy to decide where and when to break backward compatibility, but changing PHP to pass objects by reference is a fairly defensible change despite any inconveniences. The only thing you can be sure of is that any change will give rise to complaints in some quarter. Certainly, having deprecated functions issue warnings is one good way to give advance notice and let developers prepare for coming changes.

No comments: