Object-Oriented Features New to PHP 5

Chapter 3 - from the book Object-Oriented PHP by Peter Lavin -- Reprinted by permission from No Starch Press. Available at booksellers now. Full details are at the bottom of the article.

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're damned if you do and damned if you don't. 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's 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.

Where to Go from Here

If you've bought this book and read this far you're obviously interested in OOP. If you know PHP already, then learning OO PHP will not be too difficult. Given the relative simplicity of PHP's object model, certainly less effort is required than for a C programmer to learn C++. Nevertheless, moving to a new language or a new version of a language entails some cost in terms of time and effort, especially if it has an impact on your existing code libraries.

We've covered some of the backward compatibility issues as they relate to OOP. Almost all procedural code will run with no changes under PHP 5. No rewrites are required, and code does not need to be converted to an OO style.

Upgrading existing applications to take advantage of PHP 5 is a different matter. In the case of some large applications, upgrading may require significant effort. Many applications will benefit by being upgraded. If you've ever tried to customize software such as phpBB (the popular open-source forum), you know that the task would be much simpler if the application was object-oriented. However, upgrading an application such as phpBB means beginning again from scratch.

And there are other considerations besides code compatibility. After learning the ins and outs of OOP with PHP 5, will you actually be able to make use of it? Are there actually servers out there running PHP 5?

Adoption of PHP 5

As of this writing PHP 5 is hardly a bleeding-edge technology. It has been available for more than a year, and there have been a number of bug fixes. It's a stable product. Where developers have control over web server configuration there's no question that upgrading to PHP 5 will be beneficial. But developers don't always have a choice in this matter. In some situations (where the developer has no control of the web host, for instance), the decision to upgrade is in someone else's hands.

PHP is a victim of its own success. The popularity and stability of PHP 4 have slowed the adoption of PHP 5. PHP 4 is a mature language that supports many applications, open-source and otherwise. There's naturally a reluctance to rock the boat. For this reason the adoption of PHP 5 has been somewhat slow, especially in shared hosting environments.

NOTE: Other web hosting options have been much quicker to adopt PHP 5. The various virtual private server (VPS) hosting options usually include PHP 5, as do dedicated hosts. As a more secure and increasingly inexpensive hosting option, VPS is becoming much more popular.


Widespread adoption of PHP 5 will happen sooner or later, but this book recognizes that developers may need, at least for a time, to continue writing new applications that will run under PHP 4. For this reason, wherever possible, a PHP 4 version of code has been provided in addition to the PHP 5 version.

In a sense, PHP 5 just formalizes what was already possible in PHP 4. For instance, even though PHP 4 allows direct access to instance variables, when creating a class in PHP 4 it makes sense to write accessor methods for variables rather than setting or retrieving them directly. This requires a disciplined approach, but it will yield code that not only runs under PHP 4 but also will be much easier to upgrade to PHP 5. Adding restrictive access modifiers to variables will be a relatively simple task if accessor methods are already in place. Writing code with the expectation of upgrading it will also invariably mean writing better code.

That's all the talk about OOP. In the remaining chapters you're going to do OOP.

1 See Zeev Suraski, "Object-Oriented Evolution of PHP," available at www.devx.com/webdev/

Reprinted by permission from No Starch Press -- Copyright 2006 No Starch Press. All rights reserved.

            Object-Oriented PHP: Concepts, Techniques, and Code by Peter Lavin
            Published June 2006, 216 pp., ISBN 1-59327-077-1
            Website www.nostarch.com/oophp.htm



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

This book is way worth getting

Alexander's picture


as a beginner / intermediate programmer i would definitely recommend this book to anyone who is looking to improve his PHP programming skills.

although this book requires some previous skills, reading it once or twice to understand wouldn't hurt.


Interesting Read

Jonathan Zencovich's picture

Interesting Read, I have yet to use much of PHP 5. Perhaps it's time for me to give it a serious try :)

Thanks to No Starch Press for allowing this free preview to Chapter 3 of Object-Oriented PHP. I may decide to get a copy because of this post :)

-- | http://jzencovich.blogspot.com