PHP 5 Power Programming
Gutmans_Frontmatter Page i Thursday, September 23, 2004 9:05 AM
B
RUCE
P
ERENS
’ O
PEN
S
OURCE
S
ERIES
/>
◆
Java Application Development on Linux
Carl Albing and Michael Schwarz
◆
C++ GUI Programming with Qt 3
Jasmin Blanchette, Mark Summerfield
◆
Managing Linux Systems with Webmin: System Administration and
Module Development
Jamie Cameron
◆
Understanding the Linux Virtual Memory Manager
Mel Gorman
perens_series_7x9.25.fm Page 1 Wednesday, September 15, 2004 10:54 AM
Gutmans_Frontmatter Page ii Thursday, September 23, 2004 9:05 AM
PRENTICE HALL
Professional Technical Reference
Indianapolis, IN 46240
www.phptr.com
PHP 5 Power Programming
Andi Gutmans, Stig Sæther Bakken,
and Derick Rethans
Gutmans_Frontmatter Page iii Thursday, September 23, 2004 9:05 AM
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied
warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for inciden-
tal or consequential damages in connection with or arising out of the use of the information or programs con-
tained herein.
Publisher:
John Wait
Editor in Chief:
Don O’Hagan
Acquisitions Editor:
Indexer:
Lisa Stumpf
Senior Compositor:
Gloria Schurick
Manufacturing Buyer:
Dan Uhrig
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special
sales, which may include electronic versions and/or custom covers and content particular to your business,
training goals, marketing focus, and branding interests. For more information, please contact:
U. S. Corporate and Government Sales
(800) 382-3419
c
For sales outside the U. S., please contact:
International Sales
i
Visit us on the Web: www.phptr.com
Library of Congress Cataloging-in-Publication Data:
2004107331
Copyright © 2005 Pearson Education, Inc.
This material may be distrubuted only subject to the terms and conditions set forth in the Open Publication
License, v1.0 or later (the latest version is presently available at />Pearson Education, Inc.
One Lake Street
Chapter 1:
What Is New in PHP 5?
Chapter 2:
PHP 5 Basic Language
Chapter 3:
PHP 5 OO Language
Chapter 4:
PHP 5 Advanced OOP and Design Patterns
Chapter 5:
How to Write a Web Application with PHP
Chapter 6:
Databases with PHP 5
Chapter 7:
Error Handling
Chapter 8:
Chapter 16:
PHP Shell Scripting
A.
PEAR and PECL Package Index
B.
phpDocumentor Format Reference
C.
Zend Studio Quick Start
Index
Gutmans_Frontmatter Page vii Thursday, September 23, 2004 9:05 AM
Gutmans_Frontmatter Page viii Thursday, September 23, 2004 9:05 AM
ix
Contents
Foreword............................................................................................................. xxi
Preface................................................................................................................xxii
1 What Is New in PHP 5? ........................................................................................1
x Contents
2.5.6 Resources ...................................................................................................23
2.5.7 Arrays.........................................................................................................23
2.5.8 Constants ...................................................................................................30
2.6 Operators...........................................................................................................31
2.6.1 Binary Operators .......................................................................................32
2.6.2 Assignment Operators...............................................................................32
2.6.3 Comparison Operators ..............................................................................33
2.6.4 Logical Operators ......................................................................................34
2.6.5 Bitwise Operators ......................................................................................35
2.6.6 Unary Operators........................................................................................36
2.6.7 Negation Operators ...................................................................................36
2.6.8 Increment/Decrement Operators ..............................................................37
2.6.9 The Cast Operators ...................................................................................38
2.6.10 The Silence Operator...............................................................................39
2.6.11 The One and Only Ternary Operator .....................................................39
2.7 Control Structures ............................................................................................39
2.7.1 Conditional Control Structures.................................................................39
2.7.2 Loop Control Structures............................................................................42
2.7.3 Code Inclusion Control Structures ...........................................................45
2.8 Functions ...........................................................................................................48
2.8.1 User-Defined Functions ............................................................................49
2.8.2 Function Scope...........................................................................................49
2.8.3 Returning Values By Value ......................................................................50
2.8.4 Returning Values By Reference................................................................51
2.8.5 Declaring Function Parameters................................................................52
2.8.6 Static Variables .........................................................................................53
2.9 Summary ...........................................................................................................54
public
,
protected
, and
private
Methods ..........................................61
3.6.3 Static Properties ........................................................................................62
3.6.4 Static Methods ...........................................................................................64
3.7 Class Constants.................................................................................................65
3.8 Cloning Objects .................................................................................................66
3.9 Polymorphism....................................................................................................67
3.10
parent
:: and
self
::........................................................................................70
3.11
instanceof
3.20 Class Type Hints in Function Parameters ....................................................82
3.21 Summary.........................................................................................................83
4 PHP 5 Advanced OOP and Design Patterns .................................................85
4.1 Introduction ...................................................................................................... 85
4.2 Overloading Capabilities..................................................................................85
4.2.1 Property and Method Overloading ...........................................................85
4.2.2 Overloading the Array Access Syntax...................................................... 88
4.3 Iterators ............................................................................................................ 89
4.4 Design Patterns ................................................................................................ 94
4.4.1 Strategy Pattern........................................................................................95
4.4.2 Singleton Pattern ...................................................................................... 97
4.4.3 Factory Pattern ......................................................................................... 98
4.4.4 Observer Pattern..................................................................................... 101
4.5 Reflection.........................................................................................................103
4.5.1 Introduction ............................................................................................. 103
4.5.2 Reflection API..........................................................................................103
4.5.3 Reflection Examples................................................................................106
4.5.4 Implementing the Delegation Pattern Using Reflection.......................107
4.6 Summary.........................................................................................................109
5 How to Write a Web Application with PHP.................................................111
5.1 Introduction .................................................................................................... 111
5.2 Embedding into HTML .................................................................................. 112
5.3 User Input....................................................................................................... 114
5.4 Safe-Handling User Input..............................................................................117
5.4.1 Common Mistakes................................................................................... 117
5.5 Techniques to Make Scripts “Safe” ................................................................120
6.2.6 Queries .....................................................................................................154
6.2.7 Multi Statements.....................................................................................155
6.2.8 Fetching Modes........................................................................................156
6.2.9 Prepared Statements...............................................................................156
6.2.10 BLOB Handling .....................................................................................158
6.3 SQLite..............................................................................................................160
6.3.1 SQLite Strengths and Weaknesses ........................................................160
6.3.2 Best Areas of Use.....................................................................................161
6.3.3 PHP Interface ..........................................................................................162
6.4 PEAR DB .........................................................................................................176
6.4.1 Obtaining PEAR DB................................................................................176
6.4.2 Pros and Cons of Database Abstraction .................................................177
6.4.3 Which Features Are Abstracted? ............................................................177
6.4.4 Database Connections .............................................................................178
6.4.5 Executing Queries ...................................................................................180
6.4.6 Fetching Results ......................................................................................182
6.4.7 Sequences.................................................................................................184
6.4.8 Portability Features ................................................................................185
6.4.9 Abstracted Errors ....................................................................................186
6.4.10 Convenience Methods............................................................................188
6.5 Summary .........................................................................................................190
7 Error Handling.................................................................................................. 191
7.1 Introduction.....................................................................................................191
7.2 Types of Errors ................................................................................................192
7.2.1 Programming Errors ...............................................................................192
7.2.2 Undefined Symbols..................................................................................194
7.2.3 Portability Errors ....................................................................................197