Appendix: PHP 4 versus 5

It is important to understand that PHP 5 made great leaps toward becoming an object-oriented language with greater support for classes and error handling. This Short Cut uses PHP 5 for all the examples. However, each example can be converted to PHP 4 with some effort, which will not be covered in-depth here.

There are several differences between the two versions of the language to keep in mind. If you feel that you already have a firm grasp on the differences, skip this section.

Type Hinting

As of PHP 5, you can hint to the interpreter what type of object you expect to be passed to your function or method (a function inside a class). The hint can be either a class name or the reserved "array" word. For example, if we want a function to accept only arrays it is as simple as having the function signature:

function Foo(array $x)

Likewise if we have a class Bar, we can use the signature:

function Foo(Bar $x)

This will ensure that only Bar is passed to the function.

In PHP 4, mimicking this ability is rather simple, however, not as versatile:

function Foo($v) {
       if ( !is_array($x) ) { echo "Parameter must be an array"; return; }
       ...

Without the type hinting there isn't any convenient way to tell if an object is a specific type of object.

Exception Handling

Exception handling is a new and valued addition to PHP 5. This is one of the major new features of PHP 5. Exceptions are a way of catching and handling errors that may occur in our scripts. In PHP 5 it is virtually identical ...

Get Ajax with PHP 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.