O'Reilly logo

Upgrading to PHP 5 by Adam Trachtenberg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 1. Introduction

PHP 4 is a wildly popular web programming language. Web servers on over 15,000,000 domains support PHP. PHP is the most popular Apache module by almost a 2-to-1 margin. But if PHP’s so great, why do we need PHP 5?

Well, PHP 4 isn’t perfect. While it makes developing web applications quick and easy, it’s occasionally weighted down by its legacy baggage. This makes tackling some problems unnecessarily difficult.

In particular, PHP’s version of object-oriented programming (OOP) lacks many features, the MySQL extension doesn’t support the new MySQL 4.1 client protocol, and XML support is a hodgepodge.

Fortunately, PHP 5 improves on PHP 4 in three major areas:

  • Object-oriented programming

  • MySQL

  • XML

These items have all been completely rewritten, turning them from limitations into star attractions. While these changes alone warrant a new version of PHP, PHP 5 also provides:

  • SQLite for an embedded database

  • Iterators

  • Error handling using exceptions

  • Streams

  • SOAP

Some of these features, such as iterators and exceptions, are available only due to fundamental changes in PHP’s core. Others, such as streams and SQLite, are PHP 4.3 features that have matured into prime-time use in PHP 5.

This book shows you how to take advantage of these new features in your applications. Additionally, it places a special emphasis on not just telling you what’s new, but showing you how and why it’s new.

Whenever possible, there’s a direct comparison between the PHP 4 method of solving a task and the PHP 5 solution. The PHP 5 way is frequently shorter, more elegant, and provides you with greater flexibility. The before-and-after examples demonstrate in clear code what’s better about PHP 5 and provide you with concrete examples to ease the transition from PHP 4 to PHP 5.

This chapter serves as a dual introduction to both PHP 5 and Upgrading to PHP 5. It begins with a brief history of PHP that provides the motivation behind PHP 5. The next section provides a short description of all the major new features of PHP 5 and, at the same time, explains how the book is organized. This chapter ends with a discussion of how to install and configure PHP 5.

Why PHP 5?

To understand why PHP 5 came to exist, it’s necessary to quickly review the evolution of PHP as a language.

When Rasmus Lerdorf created PHP back in 1995, it wasn’t even called PHP—his Personal Home Page/Forms Interpreter language was known as PHP/FI. At the time, PHP/FI’s main focus was solving small-time web tasks: guest books, hit counters, and basic forms processing. Its major benefit was its simplicity; PHP/FI made it easy to handle all the messy tasks thrown at a web developer. Additionally, it’s C-like syntax was already understood by many programmers.

Over the next two years, PHP/FI gradually grew in popularity. However, by 1997, PHP/FI was already showing its age. As the Internet gathered steam, programmers began to create more complex applications, such as e-commerce shopping carts. PHP/FI’s quirks and limitations hindered development. It was too slow and was missing some basic features, such as for and foreach loops.

These problems caused Zeev Suraski and Andi Gutmans, of Zend fame, to begin work on a new version of PHP/FI. This version, which became PHP 3, solved many difficulties faced by PHP/FI developers while remaining true to the essential nature of PHP/FI.

In particular, PHP 3 was faster and more efficient than PHP/FI. The new parser also provided the opportunity to iron out some language oddities, making PHP more consistent. PHP 3’s other major advance was an easy-to-use extension API. Developers from all over contributed extensions to PHP, effectively turning PHP from a programming language into an entire web development environment.

PHP 3 retained PHP/FI’s procedural syntax, but it also introduced a very simplistic object-oriented syntax. Originally the result of a weekend hack, developers flocked to objects, much to the surprise of Zeev and Andi. Unfortunately, PHP 3 was ill-equipped to provide all the object-oriented features developers demanded.

A few months after PHP 3 went final in June of 1998, work started on PHP 4. Again, the problem was speed. The new extension infrastructure provided the opportunity to create larger and more complex web sites than ever imagined. In the words of Alan Greenspan, “irrational exuberance” was at hand.

While PHP 4 provided yet another burst of power, its secondary objective was backward compatibility. There was a strong emphasis on not breaking PHP 3 scripts under PHP 4. As a result, beefing up the core language was not a main focus of PHP 4. PHP 4 was released in May 2000, almost two years after PHP 3.

A lot has happened over the last four years. Perl may be the “duct tape of the Internet,” but PHP is the real glue that holds the Web together. However, PHP still faces challenges. The problems of performance and flexibility have long been conquered, but now PHP is under attack from the twin foes of Java and C#.

Over the past 10 years, Java and C# have introduced advanced object-oriented programming concepts to web development. Yet in many ways, despite all the improvements, PHP 4.3 is still the same procedural language that Rasmus wrote a decade ago. PHP 5 finally grants developers their wish, providing a full set of object-oriented features.

These features, which are discussed in Chapter 2, allow developers to more easily develop large-scale applications without resorting to the cumbersome workarounds necessary in PHP 4. They also let you write cleaner code that’s less error-prone and more maintainable.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required