Skip to Main Content
Ajax with PHP 5
book

Ajax with PHP 5

by Andrew Curioso
May 2007
Intermediate to advanced content levelIntermediate to advanced
56 pages
1h 34m
English
O'Reilly Media, Inc.
Content preview from Ajax with PHP 5

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pro PHP and jQuery

Pro PHP and jQuery

Jason Lengstorf
Expert PHP and MySQL®

Expert PHP and MySQL®

Andrew Curioso, Ronald Bradford, Patrick Galbraith

Publisher Resources

ISBN: 9780596514037Purchase LinkErrata Page