PHP 5 Power ProgrammingGutmans_Frontmatter Page i Thursday, September 23, 2004 9:05 AM
BRUCE PERENS’ OPEN SOURCE SERIEShttp://www.phptr.com/perens◆ Java Application Development on LinuxCarl Albing and Michael ...
PRENTICE HALLProfessional Technical ReferenceIndianapolis, IN 46240www.phptr.comPHP 5 Power ProgrammingAndi Gutmans, Stig ...
The authors and publisher have taken care in the preparation of this book, but make no expressed or impliedwarranty of any...
To Ifat, my wife and best friend, who has patiently put up with myinvolement in PHP from the very beginning, and has encou...
Gutmans_Frontmatter Page vi Thursday, September 23, 2004 9:05 AM
viiContentsForeword by Zeev SuraskiPreface: Introduction and BackgroundChapter 1: What Is New in PHP 5?Chapter 2: PHP 5 Ba...
Gutmans_Frontmatter Page viii Thursday, September 23, 2004 9:05 AM
ixContentsForeword...........................................................................................................
x Contents2.5.6 Resources ...................................................................................................
Contents xi3.12 Abstract Methods and Classes.......................................................................723.13 ...
xii Contents5.8 File Uploads ................................................................................................
Contents xiii7.2.4 Runtime Errors........................................................................................2...
xiv Contents9.3 Regular Expressions .........................................................................................
Contents xv10.6.14 pear config-set .............................................................................37610.6.15...
xvi Contents12.7 The package.xml Format ..............................................................................4161...
Contents xvii13.7 Other Compatibility Problems.....................................................................44313.7...
xviii Contents15.2.6 Global Variables .......................................................................................
Contents xixA.30 Tools and Utilities ........................................................................................
xx ContentsC.4.1 Studio Components.................................................................................644C.4....
xxiForewordWithin the last few years, PHP has grown to be the most widespread web plat-form in the world, operational in m...
xxii PrefacePreface“The best security against revolution is in constant correction of abuses andthe introduction of needed...
In the Beginning xxiiiPHP/FI 2A rewrite came with PHP/FI 22in 1997, but at that time the development wasalmost solely hand...
xxiv PrefaceIn PHP 3, the minor version number (the middle digit) was never used,and all versions were numbered as 3.0.x. ...
Audience xxvAUDIENCEThis book is an introduction to the advanced features new to PHP 5. It is writ-ten for PHP programmers...
xxvi PrefaceAll scripts can throw errors, but of course you do not want them to showup on your web site once your applicat...
A Note About Coding Styles xxviiChapter 15, “An Introduction to Writing PHP Extensions,” explains howto write your own cus...
xxviii PrefaceACKNOWLEDGEMENTSThis book could not have been written without feedback from our technicalreviewers; therefor...
1C H A P T E R 1What Is New in PHP 5?“The best way to be ready for the future is to invent it.”— John Sculley1.1 INTRODUCT...
2 What Is New in PHP 5? Chap. 1So, what were some of the limitations in PHP 3 and 4? The biggest limi-tation (which led to...
1.2 Language Features 3The old object model not only led to the afore-mentioned problems, butalso to fundamental problems ...
4 What Is New in PHP 5? Chap. 1☞ Interfaces.Gives the ability for a class to fulfill more than one is-a relationships. A cl...
1.2 Language Features 5class MyClass {function __clone() {print "Object is being cloned";}}$obj = new MyClass();$obj_copy ...
6 What Is New in PHP 5? Chap. 1☞ Abstract classes.A class may be declared abstract to prevent it from being instantiated.H...
1.2 Language Features 7For a more complete example, see Chapter 4, “PHP 5 Advanced OOP andDesign Patterns.”☞ __autoload()....
8 What Is New in PHP 5? Chap. 1☞ foreach with references.In PHP 4, you could not iterate through an array and modify its v...
1.3 General PHP Changes 9All the afore-mentioned extensions (SAX, DOM, and XSLT) now uselibxml2, including the new additio...
10 What Is New in PHP 5? Chap. 1</client></clients>The following code prints each client’s name and account number:$client...
1.4 Other New Features in PHP 5 11offer access to an SQL server. Contrary to what its name implies, SQLite hasmany feature...
Gutmans_Ch01 Page 12 Thursday, September 23, 2004 2:35 PM
13C H A P T E R 2PHP 5 Basic Language“A language that doesn’t have everything is actually easier toprogram in than some th...
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Php 5-power-programming
Upcoming SlideShare
Loading in …5
×

Php 5-power-programming

14,613 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
14,613
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php 5-power-programming

  1. 1. PHP 5 Power ProgrammingGutmans_Frontmatter Page i Thursday, September 23, 2004 9:05 AM
  2. 2. BRUCE PERENS’ OPEN SOURCE SERIEShttp://www.phptr.com/perens◆ Java Application Development on LinuxCarl Albing and Michael Schwarz◆ C++ GUI Programming with Qt 3Jasmin Blanchette, Mark Summerfield◆ Managing Linux Systems with Webmin: System Administration andModule DevelopmentJamie Cameron◆ Understanding the Linux Virtual Memory ManagerMel Gorman◆ Implementing CIFS: The Common Internet File SystemChristopher Hertel◆ Embedded Software Development with eCosAnthony Massa◆ Rapid Application Development with MozillaNigel McFarlane◆ The Linux Development Platform: Configuring, Using, and Maintaining aComplete Programming EnvironmentRafeeq Ur Rehman, Christopher Paul◆ Intrusion Detection with SNORT: Advanced IDS Techniques Using SNORT,Apache, MySQL, PHP, and ACIDRafeeq Ur Rehman◆ The Official Samba-3 HOWTO and Reference GuideJohn H. Terpstra, Jelmer R. Vernooij, Editors◆ Samba-3 by Example: Practical Exercises to Successful DeploymentJohn H. Terpstraperens_series_7x9.25.fm Page 1 Wednesday, September 15, 2004 10:54 AMGutmans_Frontmatter Page ii Thursday, September 23, 2004 9:05 AM
  3. 3. PRENTICE HALLProfessional Technical ReferenceIndianapolis, IN 46240www.phptr.comPHP 5 Power ProgrammingAndi Gutmans, Stig Sæther Bakken,and Derick RethansGutmans_Frontmatter Page iii Thursday, September 23, 2004 9:05 AM
  4. 4. The authors and publisher have taken care in the preparation of this book, but make no expressed or impliedwarranty 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 WaitEditor in Chief: Don O’HaganAcquisitions Editor: Mark L. TaubEditorial Assistant: Noreen ReginaDevelopment Editor:Janet ValadeMarketing Manager: Robin OBrienCover Designer: Nina ScuderiManaging Editor: Gina KanouseSenior Project Editor: Kristy HartCopy Editor: Specialized CompositionIndexer: Lisa StumpfSenior Compositor: Gloria SchurickManufacturing Buyer: Dan UhrigThe publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or specialsales, 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-3419corpsales@pearsontechgroup.comFor sales outside the U. S., please contact:International Salesinternational@pearsoned.comVisit us on the Web: www.phptr.comLibrary of Congress Cataloging-in-Publication Data:2004107331Copyright © 2005 Pearson Education, Inc.This material may be distrubuted only subject to the terms and conditions set forth in the Open PublicationLicense, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).Pearson Education, Inc.One Lake StreetUpper Saddle River, NJ 07458Every effort was made to contact and credit all copyright holders. Use of material without proper creditis unintentional.ISBN 0-131-47149-XText printed in the United States on recycled paper at Phoenix in Hagerstown, Maryland.First printing, [October 2004]Gutmans_Frontmatter Page iv Thursday, September 23, 2004 2:14 PM
  5. 5. To Ifat, my wife and best friend, who has patiently put up with myinvolement in PHP from the very beginning, and has encouragedand supported me every step of the way.Andi GutmansTo Marianne, for patience and encouragement.Stig Sæther BakkenTo my parents, who care for me even when I’m not around;and to 42, the answer to life,the universe of everything.Derick RethansGutmans_Frontmatter Page v Thursday, September 23, 2004 9:05 AM
  6. 6. Gutmans_Frontmatter Page vi Thursday, September 23, 2004 9:05 AM
  7. 7. viiContentsForeword by Zeev SuraskiPreface: Introduction and BackgroundChapter 1: What Is New in PHP 5?Chapter 2: PHP 5 Basic LanguageChapter 3: PHP 5 OO LanguageChapter 4: PHP 5 Advanced OOP and Design PatternsChapter 5: How to Write a Web Application with PHPChapter 6: Databases with PHP 5Chapter 7: Error HandlingChapter 8: XML with PHP 5Chapter 9: Mainstream ExtensionsChapter 10: Using PEARChapter 11: Important PEAR PackagesChapter 12: Building PEAR ComponentsChapter 13: Making the MoveChapter 14: PerformanceChapter 15: An Introduction to Writing PHP ExtensionsChapter 16: PHP Shell ScriptingA. PEAR and PECL Package IndexB. phpDocumentor Format ReferenceC. Zend Studio Quick StartIndexGutmans_Frontmatter Page vii Thursday, September 23, 2004 9:05 AM
  8. 8. Gutmans_Frontmatter Page viii Thursday, September 23, 2004 9:05 AM
  9. 9. ixContentsForeword............................................................................................................. xxiPreface................................................................................................................xxii1 What Is New in PHP 5? ........................................................................................11.1 Introduction ........................................................................................................11.2 Language Features .............................................................................................11.2.1 New Object-Oriented Model........................................................................11.2.2 New Object-Oriented Features...................................................................31.2.3 Other New Language Features ..................................................................71.3 General PHP Changes........................................................................................81.3.1 XML and Web Services ...............................................................................81.4 Other New Features in PHP 5 .........................................................................111.4.1 New Memory Manager..............................................................................111.4.2 Dropped Support for Windows 95.............................................................111.5 Summary...........................................................................................................112 PHP 5 Basic Language.......................................................................................132.1 Introduction ......................................................................................................132.2 HTML Embedding............................................................................................142.3 Comments .........................................................................................................142.4 Variables............................................................................................................152.4.1 Indirect References to Variables ..............................................................162.4.2 Managing Variables ..................................................................................162.4.3 Superglobals ..............................................................................................182.5 Basic Data Types ..............................................................................................182.5.1 Integers......................................................................................................192.5.2 Floating-Point Numbers ...........................................................................192.5.3 Strings........................................................................................................192.5.4 Booleans.....................................................................................................222.5.5 Null ............................................................................................................23Gutmans_TOC Page ix Thursday, September 23, 2004 9:06 AM
  10. 10. x Contents2.5.6 Resources ...................................................................................................232.5.7 Arrays.........................................................................................................232.5.8 Constants ...................................................................................................302.6 Operators...........................................................................................................312.6.1 Binary Operators.......................................................................................322.6.2 Assignment Operators...............................................................................322.6.3 Comparison Operators ..............................................................................332.6.4 Logical Operators ......................................................................................342.6.5 Bitwise Operators......................................................................................352.6.6 Unary Operators........................................................................................362.6.7 Negation Operators ...................................................................................362.6.8 Increment/Decrement Operators ..............................................................372.6.9 The Cast Operators ...................................................................................382.6.10 The Silence Operator...............................................................................392.6.11 The One and Only Ternary Operator .....................................................392.7 Control Structures ............................................................................................392.7.1 Conditional Control Structures.................................................................392.7.2 Loop Control Structures............................................................................422.7.3 Code Inclusion Control Structures ...........................................................452.8 Functions...........................................................................................................482.8.1 User-Defined Functions ............................................................................492.8.2 Function Scope...........................................................................................492.8.3 Returning Values By Value ......................................................................502.8.4 Returning Values By Reference................................................................512.8.5 Declaring Function Parameters................................................................522.8.6 Static Variables .........................................................................................532.9 Summary ...........................................................................................................543 PHP 5 OO Language .......................................................................................... 553.1 Introduction.......................................................................................................553.2 Objects ...............................................................................................................553.3 Declaring a Class ..............................................................................................573.4 The new Keyword and Constructors ................................................................573.5 Destructors........................................................................................................583.6 Accessing Methods and Properties Using the $this Variable.......................593.6.1 public, protected, and private Properties .......................................603.6.2 public, protected, and private Methods ..........................................613.6.3 Static Properties ........................................................................................623.6.4 Static Methods ...........................................................................................643.7 Class Constants.................................................................................................653.8 Cloning Objects .................................................................................................663.9 Polymorphism....................................................................................................673.10 parent:: and self::........................................................................................703.11 instanceof Operator....................................................................................71Gutmans_TOC Page x Thursday, September 23, 2004 9:06 AM
  11. 11. Contents xi3.12 Abstract Methods and Classes.......................................................................723.13 Interfaces ........................................................................................................733.14 Inheritance of Interfaces ................................................................................753.15 final Methods ...............................................................................................753.16 final Classes.................................................................................................763.17 __toString() Method..................................................................................763.18 Exception Handling........................................................................................773.19 __autoload() ...............................................................................................803.20 Class Type Hints in Function Parameters ....................................................823.21 Summary.........................................................................................................834 PHP 5 Advanced OOP and Design Patterns .................................................854.1 Introduction ......................................................................................................854.2 Overloading Capabilities..................................................................................854.2.1 Property and Method Overloading...........................................................854.2.2 Overloading the Array Access Syntax......................................................884.3 Iterators ............................................................................................................894.4 Design Patterns ................................................................................................944.4.1 Strategy Pattern........................................................................................954.4.2 Singleton Pattern ......................................................................................974.4.3 Factory Pattern .........................................................................................984.4.4 Observer Pattern.....................................................................................1014.5 Reflection.........................................................................................................1034.5.1 Introduction.............................................................................................1034.5.2 Reflection API..........................................................................................1034.5.3 Reflection Examples................................................................................1064.5.4 Implementing the Delegation Pattern Using Reflection.......................1074.6 Summary.........................................................................................................1095 How to Write a Web Application with PHP.................................................1115.1 Introduction ....................................................................................................1115.2 Embedding into HTML ..................................................................................1125.3 User Input.......................................................................................................1145.4 Safe-Handling User Input..............................................................................1175.4.1 Common Mistakes...................................................................................1175.5 Techniques to Make Scripts “Safe” ................................................................1205.5.1 Input Validation ......................................................................................1205.5.2 HMAC Verification..................................................................................1225.5.3 PEAR::Crypt_HMAC...............................................................................1245.5.4 Input Filter ..............................................................................................1275.5.5 Working with Passwords ........................................................................1275.5.6 Error Handling........................................................................................1295.6 Cookies ............................................................................................................1315.7 Sessions...........................................................................................................134Gutmans_TOC Page xi Thursday, September 23, 2004 9:06 AM
  12. 12. xii Contents5.8 File Uploads ....................................................................................................1375.8.1 Handling the Incoming Uploaded File ...................................................1385.9 Architecture.....................................................................................................1435.9.1 One Script Serves All ..............................................................................1435.9.2 One Script per Function..........................................................................1445.9.3 Separating Logic from Layout ................................................................1445.10 Summary .......................................................................................................1466 Databases with PHP 5 ..................................................................................... 1496.1 Introduction.....................................................................................................1496.2 MySQL.............................................................................................................1496.2.1 MySQL Strengths and Weaknesses .......................................................1506.2.2 PHP Interface ..........................................................................................1506.2.3 Example Data ..........................................................................................1516.2.4 Connections..............................................................................................1516.2.5 Buffered Versus Unbuffered Queries .....................................................1536.2.6 Queries .....................................................................................................1546.2.7 Multi Statements.....................................................................................1556.2.8 Fetching Modes........................................................................................1566.2.9 Prepared Statements...............................................................................1566.2.10 BLOB Handling .....................................................................................1586.3 SQLite..............................................................................................................1606.3.1 SQLite Strengths and Weaknesses ........................................................1606.3.2 Best Areas of Use.....................................................................................1616.3.3 PHP Interface ..........................................................................................1626.4 PEAR DB.........................................................................................................1766.4.1 Obtaining PEAR DB................................................................................1766.4.2 Pros and Cons of Database Abstraction .................................................1776.4.3 Which Features Are Abstracted?............................................................1776.4.4 Database Connections .............................................................................1786.4.5 Executing Queries ...................................................................................1806.4.6 Fetching Results ......................................................................................1826.4.7 Sequences.................................................................................................1846.4.8 Portability Features ................................................................................1856.4.9 Abstracted Errors ....................................................................................1866.4.10 Convenience Methods............................................................................1886.5 Summary .........................................................................................................1907 Error Handling.................................................................................................. 1917.1 Introduction.....................................................................................................1917.2 Types of Errors................................................................................................1927.2.1 Programming Errors ...............................................................................1927.2.2 Undefined Symbols..................................................................................1947.2.3 Portability Errors ....................................................................................197Gutmans_TOC Page xii Thursday, September 23, 2004 9:06 AM
  13. 13. Contents xiii7.2.4 Runtime Errors........................................................................................2017.2.5 PHP Errors ..............................................................................................2017.3 PEAR Errors ...................................................................................................2067.3.1 The PEAR_Error Class ...........................................................................2097.3.2 Handling PEAR Errors ...........................................................................2127.3.3 PEAR Error Modes..................................................................................2137.3.4 Graceful Handling...................................................................................2137.4 Exceptions.......................................................................................................2167.4.1 What Are Exceptions?.............................................................................2167.4.2 try, catch, and throw ...............................................................................2167.5 Summary.........................................................................................................2188 XML with PHP 5 ................................................................................................2198.1 Introduction ....................................................................................................2198.2 Vocabulary.......................................................................................................2208.3 Parsing XML ...................................................................................................2228.3.1 SAX ..........................................................................................................2228.3.2 DOM.........................................................................................................2268.4 SimpleXML .....................................................................................................2318.4.1 Creating a SimpleXML Object................................................................2328.4.2 Browsing SimpleXML Objects................................................................2338.4.3 Storing SimpleXML Objects ...................................................................2348.5 PEAR...............................................................................................................2348.5.1 XML_Tree ................................................................................................2358.5.2 XML_RSS.................................................................................................2368.6 Converting XML .............................................................................................2398.6.1 XSLT ........................................................................................................2398.7 Communicating with XML.............................................................................2448.7.1 XML-RPC.................................................................................................2448.7.2 SOAP........................................................................................................2528.8 Summary.........................................................................................................2599 Mainstream Extensions ...................................................................................2619.1 Introduction ....................................................................................................2619.2 Files and Streams...........................................................................................2619.2.1 File Access ...............................................................................................2629.2.2 Program Input/Output............................................................................2649.2.3 Input/Output Streams.............................................................................2679.2.4 Compression Streams .............................................................................2689.2.5 User Streams...........................................................................................2709.2.6 URL Streams...........................................................................................2719.2.7 Locking ....................................................................................................2769.2.8 Renaming and Removing Files...............................................................2779.2.9 Temporary Files ......................................................................................278Gutmans_TOC Page xiii Thursday, September 23, 2004 9:06 AM
  14. 14. xiv Contents9.3 Regular Expressions .......................................................................................2799.3.1 Syntax ......................................................................................................2799.3.2 Functions..................................................................................................2939.4 Date Handling.................................................................................................3019.4.1 Retrieving Date and Time Information..................................................3019.4.2 Formatting Date and Time .....................................................................3059.4.3 Parsing Date Formats .............................................................................3139.5 Graphics Manipulation with GD....................................................................3149.5.1 Case 1: Bot-Proof Submission Forms .....................................................3159.5.2 Case 2: Bar Chart ....................................................................................3209.5.3 Exif..........................................................................................................3269.6 Multi-Byte Strings and Character Sets.........................................................3299.6.1 Character Set Conversions......................................................................3309.6.2 Extra Functions Dealing with Multi-Byte Character Sets ...................3359.6.3 Locales......................................................................................................3409.7 Summary .........................................................................................................34310 Using PEAR........................................................................................................ 34510.1 Introduction...................................................................................................34510.2 PEAR Concepts .............................................................................................34610.2.1 Packages.................................................................................................34610.2.2 Releases..................................................................................................34610.2.3 Version Numbers...................................................................................34710.3 Obtaining PEAR............................................................................................34910.3.1 Installing with UNIX / Linux PHP Distribution .................................35010.3.2 Installing with PHP Windows Installer...............................................35110.3.3 go-pear.org .............................................................................................35110.4 Installing Packages.......................................................................................35410.4.1 Using the pear Command ...................................................................35410.5 Configuration Parameters ............................................................................35810.6 PEAR Commands..........................................................................................36410.6.1 pear install ......................................................................................36410.6.2 pear list.............................................................................................36810.6.3 pear info.............................................................................................36910.6.4 pear list-all ....................................................................................37010.6.5 pear list-upgrades .........................................................................37010.6.6 pear upgrade ......................................................................................37110.6.7 pear upgrade-all..............................................................................37210.6.8 pear uninstall..................................................................................37310.6.9 pear search ........................................................................................37310.6.10 pear remote-list............................................................................37410.6.11 pear remote-info............................................................................37510.6.12 pear download ..................................................................................37510.6.13 pear config-get..............................................................................376Gutmans_TOC Page xiv Thursday, September 23, 2004 9:06 AM
  15. 15. Contents xv10.6.14 pear config-set .............................................................................37610.6.15 pear config-show ...........................................................................37610.6.16 Shortcuts..............................................................................................37710.7 Installer Front-Ends.....................................................................................37810.7.1 CLI (Command Line Interface) Installer.............................................37810.7.2 Gtk Installer ..........................................................................................37810.8 Summary.......................................................................................................38111 Important PEAR Packages .............................................................................38311.1 Introduction ..................................................................................................38311.2 Database Queries..........................................................................................38311.3 Template Systems.........................................................................................38311.3.1 Template Terminology ..........................................................................38411.3.2 HTML_Template_IT .............................................................................38411.3.3 HTML_Template_Flexy.......................................................................38711.4 Authentication ..............................................................................................39211.4.1 Overview ................................................................................................39211.4.2 Example: Auth with Password File......................................................39311.4.3 Example: Auth with DB and User Data ..............................................39411.4.4 Auth Security Considerations...............................................................39611.4.5 Auth Scalability Considerations...........................................................39711.4.6 Auth Summary ......................................................................................39811.5 Form Handling..............................................................................................39811.5.1 HTML_QuickForm..................................................................................39811.5.2 Example: Login Form............................................................................39911.5.3 Receiving Data.......................................................................................39911.6 Caching..........................................................................................................39911.6.1 Cache_Lite ..........................................................................................39911.7 Summary.......................................................................................................40112 Building PEAR Components ..........................................................................40312.1 Introduction ..................................................................................................40312.2 PEAR Standards...........................................................................................40312.2.1 Symbol Naming .....................................................................................40312.2.2 Indentation ............................................................................................40612.3 Release Versioning........................................................................................40812.4 CLI Environment..........................................................................................40812.5 Fundamentals...............................................................................................41012.5.1 When and How to Include Files............................................................41012.5.2 Error Handling......................................................................................41112.6 Building Packages ........................................................................................41112.6.1 PEAR Example: HelloWorld.................................................................41112.6.2 Building the Tarball..............................................................................41412.6.3 Verification ............................................................................................41412.6.4 Regression Tests....................................................................................416Gutmans_TOC Page xv Thursday, September 23, 2004 9:06 AM
  16. 16. xvi Contents12.7 The package.xml Format ..............................................................................41612.7.1 Package Information .............................................................................41712.7.2 Release Information ..............................................................................41912.8 Dependencies.................................................................................................42312.8.1 Element: <deps> ...................................................................................42312.8.2 Element: <dep> .....................................................................................42312.8.3 Dependency Types .................................................................................42412.8.4 Reasons to Avoid Dependencies............................................................42512.8.5 Optional Dependencies..........................................................................42612.8.6 Some Examples......................................................................................42612.9 String Substitutions......................................................................................42712.9.1 Element: <replace>.............................................................................42712.9.2 Examples................................................................................................42712.10 Including C Code.........................................................................................42812.10.1 Element: <configureoptions> .......................................................42812.10.2 Element: <configureoption> .........................................................42812.11 Releasing Packages.....................................................................................42812.12 The PEAR Release Process.........................................................................42912.13 Packaging ....................................................................................................43012.13.1 Source Analysis....................................................................................43012.13.2 MD5 Checksum Generation................................................................43012.13.3 Package.xml Update............................................................................43112.13.4 Tarball Creation ..................................................................................43112.14 Uploading ....................................................................................................43212.14.1 Upload Release ....................................................................................43212.14.2 Finished!...............................................................................................43212.15 Summary .....................................................................................................43213 Making the Move .............................................................................................. 43313.1 Introduction...................................................................................................43313.2 The Object Model ..........................................................................................43313.3 Passing Objects to Functions........................................................................43313.4 Compatibility Mode.......................................................................................43513.4.1 Casting Objects......................................................................................43513.4.2 Comparing Objects ................................................................................43613.5 Other Changes ..............................................................................................43713.5.1 Assigning to $this................................................................................43713.5.2 get_class.............................................................................................44013.6 E_STRICT ......................................................................................................44113.6.1 Automagically Creating Objects ...........................................................44113.6.2 var and public ....................................................................................44113.6.3 Constructors...........................................................................................44213.6.4 Inherited Methods .................................................................................44213.6.5 Define Classes Before Usage.................................................................443Gutmans_TOC Page xvi Thursday, September 23, 2004 9:06 AM
  17. 17. Contents xvii13.7 Other Compatibility Problems.....................................................................44313.7.1 Command-Line Interface......................................................................44313.7.2 Comment Tokens...................................................................................44313.7.3 MySQL ...................................................................................................44513.8 Changes in Functions...................................................................................44513.8.1 array_merge()....................................................................................44513.8.2 strrpos() and strripos()...............................................................44613.9 Summary.......................................................................................................44714 Performance .......................................................................................................44914.1 Introduction ..................................................................................................44914.2 Design for Performance................................................................................44914.2.1 PHP Design Tip #1: Beware of State ...................................................45014.2.2 PHP Design Tip #2: Cache!...................................................................45114.2.3 PHP Design Tip #3: Do Not Over Design!............................................45614.3 Benchmarking...............................................................................................45714.3.1 Using ApacheBench ..............................................................................45714.3.2 Using Siege ............................................................................................45814.3.3 Testing Versus Real Traffic ..................................................................45914.4 Profiling with Zend Studios Profiler ...........................................................45914.5 Profiling with APD........................................................................................46114.5.1 Installing APD.......................................................................................46114.5.2 Analyzing Trace Data ...........................................................................46214.6 Profiling with Xdebug...................................................................................46514.6.1 Installing Xdebug ..................................................................................46614.6.2 Tracing Script Execution ......................................................................46614.6.3 Using KCachegrind ...............................................................................46814.7 Using APC (Advanced PHP Cache) .............................................................47014.8 Using ZPS (Zend Performance Suite)..........................................................47014.8.1 Automatic Optimization........................................................................47114.8.2 Compiled Code Caching ........................................................................47214.8.3 Dynamic Content Caching....................................................................47314.8.4 Content Compression............................................................................47614.9 Optimizing Code ...........................................................................................47714.9.1 Micro-Benchmarks ................................................................................47714.9.2 Rewrite in C...........................................................................................47914.9.3 OO Versus Procedural Code .................................................................48014.10 Summary.....................................................................................................48115 An Introduction to Writing PHP Extensions..............................................48315.1 Introduction ..................................................................................................48315.2 Quickstart .....................................................................................................48415.2.1 Memory Management ...........................................................................48915.2.2 Returning Values from PHP Functions ...............................................49015.2.3 Completing self-concat()................................................................49015.2.4 Summary of Example............................................................................49215.2.5 Wrapping Third-Party Extensions .......................................................492Gutmans_TOC Page xvii Thursday, September 23, 2004 9:06 AM
  18. 18. xviii Contents15.2.6 Global Variables ....................................................................................50115.2.7 Adding Custom INI Directives..............................................................50315.2.8 Thread-Safe Resource Manager Macros...............................................50415.3 Summary .......................................................................................................50516 PHP Shell Scripting......................................................................................... 50716.1 Introduction...................................................................................................50716.2 PHP CLI Shell Scripts..................................................................................50816.2.1 How CLI Differs From CGI...................................................................50816.2.2 The Shell-Scripting Environment.........................................................51016.2.3 Parsing Command-Line Options ..........................................................51216.2.4 Good Practices........................................................................................51516.2.5 Process Control ......................................................................................51616.2.6 Examples................................................................................................52016.3 Summary .......................................................................................................526A PEAR and PECL Package Index................................................................... 527A.1 Authentication................................................................................................527A.2 Benchmarking ................................................................................................530A.3 Caching ...........................................................................................................530A.4 Configuration..................................................................................................531A.5 Console............................................................................................................531A.6 Database .........................................................................................................533A.7 Date and Time ................................................................................................542A.8 Encryption ......................................................................................................543A.9 File Formats....................................................................................................545A.10 File System ...................................................................................................548A.11 Gtk Components...........................................................................................550A.12 HTML............................................................................................................550A.13 HTTP.............................................................................................................561A.14 Images...........................................................................................................563A.15 Internationalization .....................................................................................566A.16 Logging..........................................................................................................568A.17 Mail ...............................................................................................................569A.18 Math..............................................................................................................571A.19 Networking ...................................................................................................574A.20 Numbers........................................................................................................584A.21 Payment ........................................................................................................585A.22 PEAR............................................................................................................587A.23 PHP ...............................................................................................................588A.24 Processing .....................................................................................................594A.25 Science...........................................................................................................594A.26 Streams.........................................................................................................595A.27 Structures .....................................................................................................596A.28 System...........................................................................................................598A.29 Text................................................................................................................599Gutmans_TOC Page xviii Thursday, September 23, 2004 9:06 AM
  19. 19. Contents xixA.30 Tools and Utilities ........................................................................................600A.31 Web Services.................................................................................................603A.32 XML ..............................................................................................................604B phpDocumentor Format Reference..............................................................613B.1 Introduction....................................................................................................613B.2 Documentation Comments ............................................................................613B.3 Tag Reference .................................................................................................615B.3.1 abstract................................................................................................615B.3.2 access ....................................................................................................616B.3.3 author ....................................................................................................617B.3.4 category................................................................................................618B.3.5 copyright..............................................................................................618B.3.6 deprecated............................................................................................618B.3.7 example ..................................................................................................619B.3.8 filesource............................................................................................620B.3.9 final ......................................................................................................620B.3.10 global ................................................................................................621B.3.11 ignore ................................................................................................622B.3.12 inheritdoc (inline) ..........................................................................622B.3.13 internal, internal (inline) ........................................................622B.3.14 licence..............................................................................................623B.3.15 link ....................................................................................................623B.3.16 link (inline) ......................................................................................623B.3.17 name ....................................................................................................624B.3.18 package..............................................................................................624B.3.19 param ..................................................................................................626B.3.20 return ................................................................................................627B.3.21 see........................................................................................................627B.3.22 since ..................................................................................................628B.3.23 static .................................................................................................628B.3.24 staticvar...........................................................................................629B.3.25 subpackage.........................................................................................629B.3.26 todo......................................................................................................630B.3.27 uses ....................................................................................................630B.3.28 var........................................................................................................631B.3.29 version ...............................................................................................631B.4 Tag Table.........................................................................................................632B.5 Using the phpDocumentor Tool.....................................................................633C Zend Studio Quick Start Guide ....................................................................643C.1 Version 3.5.x ...................................................................................................643C.2 About the Zend Studio Client Quick Start Guide.........................................643C.3 About Zend .....................................................................................................643C.4 Zend Studio Client: Overview........................................................................644Gutmans_TOC Page xix Thursday, September 23, 2004 9:06 AM
  20. 20. xx ContentsC.4.1 Studio Components.................................................................................644C.4.2 Client Server Configuration ...................................................................645C.4.3 Installation and Registration .................................................................645C.5 Editing a File ..................................................................................................647C.5.1 Editing a File...........................................................................................647C.6 Working with Projects ....................................................................................648C.6.1 Advantages of Working with Projects....................................................648C.6.2 How to Create a Project..........................................................................648C.7 Running the Debugger ..................................................................................648C.7.1 Internal Debugger...................................................................................649C.7.2 Remote Debugger....................................................................................649C.7.3 Debug URL..............................................................................................650C.8 Configure Studio Server for Remote Debugger and Profiling......................650C.9 Running the Profiler.......................................................................................651C.10 Product Support............................................................................................652C.10.1 Getting Support.....................................................................................653C.11 Main Features...............................................................................................653Index.................................................................................................................... 655Gutmans_TOC Page xx Thursday, September 23, 2004 9:06 AM
  21. 21. xxiForewordWithin the last few years, PHP has grown to be the most widespread web plat-form in the world, operational in more than a third of the web servers acrossthe globe. PHPs growth is not only quantitative but also qualitative. More andmore companies, including Fortune companies, rely on PHP to run their busi-ness-critical applications, which creates new jobs and increases the demandfor PHP developers. Version 5, due to be released in the very near future, holdsan even greater promise.While the complexity of starting off with PHP remains unchanged andvery low, the features offered by PHP today enable developers to reach farbeyond simple HTML applications. The revised object model allows for large-scale projects to be written efficiently, using standard object-oriented method-ologies. New XML support makes PHP the best language available for pro-cessing XML and, coupled with new SOAP support, an ideal platform forcreating and using Web Services.This book, written by my colleague, Andi Gutmans, and two very promi-nent PHP developers, Stig Bakken and Derick Rethans, holds the key tounlocking the riches of PHP 5. It thoroughly covers all of the features of thenew version, and is a must-have for all PHP developers who are interested inexploring PHP 5s advanced features.Zeev SuraskiGutmans_PrefaceFore Page xxi Thursday, September 23, 2004 9:06 AM
  22. 22. xxii PrefacePreface“The best security against revolution is in constant correction of abuses andthe introduction of needed improvements. It is the neglect of timely repairthat makes rebuilding necessary.”—Richard WhatelyIN THE BEGINNINGIt was eight years ago, when Rasmus Lerdorf first started developing PHP/FI.He could not have imagined that his creation would eventually lead to thedevelopment of PHP as we know it today, which is being used by millions ofpeople. The first version of “PHP/FI,” called Personal Homepage Tools/Form Interpreter, was a collection of Perl scripts in 1995.1One of the basicfeatures was a Perl-like language for handling form submissions, but it lackedmany common useful language features, such as for loops.1http://groups.google.com/groups?selm=3r7pgp$aa1@ionews.io.org.Gutmans_PrefaceFore Page xxii Thursday, September 23, 2004 9:06 AM
  23. 23. In the Beginning xxiiiPHP/FI 2A rewrite came with PHP/FI 22in 1997, but at that time the development wasalmost solely handled by Rasmus. After its release in November of that year,Andi Gutmans and Zeev Suraski bumped into PHP/FI while looking for a lan-guage to develop an e-commerce solution as a university project. They discov-ered that PHP/FI was not quite as powerful as it seemed, and its language waslacking many common features. One of the most interesting aspects includedthe way while loops were implemented. The hand-crafted lexical scanner wouldgo through the script and when it hit the while keyword it would remember itsposition in the file. At the end of the loop, the file pointer sought back to thesaved position, and the whole loop was reread and re-executed.PHP 3Zeev and Andi decided to completely rewrite the scripting language. They thenteamed up with Rasmus to release PHP 3, and along also came a new name: PHP:Hypertext Preprocessor, to emphasize that PHP was a different product and notonly suitable for personal use. Zeev and Andi had also designed and implementeda new extension API. This new API made it possible to easily support additionalextensions for performing tasks such as accessing databases, spell checkers andother technologies, which attracted many developers who were not part of the“core” group to join and contribute to the PHP project. At the time of PHP 3’srelease3in June 1998, the estimated PHP installed base consisted of about 50,000domains. PHP 3 sparked the beginning of PHP’s real breakthrough, and was thefirst version to have an installed base of more than one million domains.PHP 4In late 1998, Zeev and Andi looked back at their work in PHP 3 and felt theycould have written the scripting language even better, so they started yetanother rewrite. While PHP 3 still continuously parsed the scripts while execut-ing them, PHP 4 came with a new paradigm of “compile first, execute later.” Thecompilation step does not compile PHP scripts into machine code; it insteadcompiles them into byte code, which is then executed by the Zend Engine(Zend stands for Zeev & Andi), the new heart of PHP 4. Because of this newway of executing scripts, the performance of PHP 4 was much better than thatof PHP 3, with only a small amount of backward compatibility breakage4.Among other improvements was an improved extension API for better run-timeperformance, a web server abstraction layer allowing PHP 4 to run on most pop-ular web servers, and lots more. PHP 4 was officially released on May 22, 2002,and today its installed base has surpassed 15 million domains.2http://groups.google.com/groups?selm=Dn1JM9.61t%40gpu.utcc.utoronto.ca.3http://groups.google.com/groups?selm=Pine.WNT.3.96.980606130654.-317675I-100000%40shell.lerdorf.on.ca.4http://www.php.net/manual/en/migration4.php.Gutmans_PrefaceFore Page xxiii Thursday, September 23, 2004 9:06 AM
  24. 24. xxiv PrefaceIn PHP 3, the minor version number (the middle digit) was never used,and all versions were numbered as 3.0.x. This changed in PHP 4, and the minorversion number was used to denote important changes in the language. The firstimportant change came in PHP 4.1.0,5which introduced superglobals such as$_GET and $_POST. Superglobals can be accessed from within functions withouthaving to use the global keyword. This feature was added in order to allow theregister_globals INI option to be turned off. register_globals is a feature inPHP which automatically converts input variables like "?foo=bar" in http://php.net/?foo=bar to a PHP variable called $foo. Because many people do notcheck input variables properly, many applications had security holes, whichmade it quite easy to circumvent security and authentication code.With the new superglobals in place, on April 22, 2002, PHP 4.2.0 wasreleased with the register_globals turned off by default. PHP 4.3.0, the lastsignificant PHP 4 version, was released on December 27, 2002. This versionintroduced the Command Line Interface (CLI), a revamped file and net-work I/O layer (called streams), and a bundled GD library. Although most ofthose additions have no real effect on end users, the major version wasbumped due to the major changes in PHP’s core.PHP 5Soon after, the demand for more common object-oriented features increasedimmensely, and Andi came up with the idea of rewriting the objected-orientedpart of the Zend Engine. Zeev and Andi wrote the “Zend Engine II: FeatureOverview and Design” document6and jumpstarted heated discussions aboutPHP’s future. Although the basic language has stayed the same, many fea-tures were added, dropped, and changed by the time PHP 5 matured. Forexample, namespaces and multiple inheritance, which were mentioned in theoriginal document, never made it into PHP 5. Multiple inheritance wasdropped in favor of interfaces, and namespaces were dropped completely. Youcan find a full list of new features in Chapter, “What Is New in PHP 5?”PHP 5 is expected to maintain and even increase PHP’s leadership inthe web development market. Not only does it revolutionizes PHP’s object-oriented support but it also contains many new features which make it theultimate web development platform. The rewritten XML functionality inPHP 5 puts it on par with other web technologies in some areas and over-takes them in others, especially due to the new SimpleXML extension whichmakes it ridiculously easy to manipulate XML documents. In addition, thenew SOAP, MySQLi, and variety of other extensions are significant mile-stones in PHP’s support for additional technologies.5http://www.php.net/release_4_1_0.php.6http://zend.com/engine2/ZendEngine-2.0.pdf.Gutmans_PrefaceFore Page xxiv Thursday, September 23, 2004 9:06 AM
  25. 25. Audience xxvAUDIENCEThis book is an introduction to the advanced features new to PHP 5. It is writ-ten for PHP programmers who are making the move to PHP 5. AlthoughChapter 2, “PHP 5 Basic Language,” contains an introduction to PHP 5 syn-tax, it is meant as a refresher for PHP programmers and not as a tutorial fornew programmers. However, web developers with experience programmingother high-level languages may indeed find that this tutorial is all they needin order to begin working effectively with PHP 5.CHAPTER OVERVIEWChapter 1, “What Is New in PHP 5?” discusses the new features in PHP 5.Most of these new features deal with new object-oriented features, includingsmall examples for each feature. It also gives an overview of the new exten-sions in PHP 5. Most of the topics mentioned in this chapter are explained inmore detail in later chapters.Chapter 2, “PHP 5 Basic Language,” introduces the PHP syntax to thosereaders not familiar with PHP. All basic language constructs and variabletypes are explained along with simple examples to give the reader the neces-sary building blocks to build real scripts.Chapter 3, “PHP 5 OO Language,” continues exploring PHP 5s syntax,focusing on its object-oriented functionality. This chapter covers basics, suchas properties and methods, and progresses to more complicated subjects, suchas polymorphism, interfaces, exceptions, and lots more.Using the previous chapter as a foundation, Chapter 4, “PHP 5 AdvancedOOP and Design Patterns,” covers some of the most advanced features of PHP5’s object model. After learning these features, including four commonly useddesign patterns and PHP’s reflection capabilities, you will soon become an OOwizard.Now that you are familiar with the syntax and language features of PHP,Chapter 5, “How to Write a Web Application with PHP,” introduces you to theworld of writing web applications. The authors show you basics, such as han-dling input through form variables and safety techniques, but this chapteralso includes more advanced topics, such as handling sessions with cookiesand PHPs session extension. You also find a few tips on laying out your sourcecode for your web applications.Chapter 6, “Databases with PHP 5,” introduces using MySQL, SQLite,and Oracle from PHP, but focuses primarily on the PHP 5-specific details ofdatabase access. For each database, you learn about some of its strong andweak points, as well as the types of applications at which each excels. And ofcourse, you learn how to interface with them using PHPs native functions orusing PEAR DB.Gutmans_PrefaceFore Page xxv Thursday, September 23, 2004 9:06 AM
  26. 26. xxvi PrefaceAll scripts can throw errors, but of course you do not want them to showup on your web site once your application has passed its development state.Chapter 7, “Error Handling,” deals with different types of errors that exist,how to handle those errors with PHP, and how to handle errors with PEAR.As one of the important new features in PHP 5 is its renewed XML sup-port, a chapter on XML features in PHP 5 could not be missed. Chapter 8,“XML with PHP 5,” talks about the different strategies of parsing XML andconverting XML to other formats with XSLT. XML-RPC and SOAP are intro-duced to show you how to implement web services with both techniques.Although not specifically for PHP 5, the five mainstream extensions thatChapter 9,“Mainstream Extensions,” covers are important enough to deserve aplace in this book. The first section, “Files and Streams,” explains about han-dling files and network streams. A stream is nothing more than a way toaccess external data, such as a file, remote URL, or compressed file. The sec-ond section, “Regular Expressions,” explains the syntax of a regular expres-sion engine (PCRE) that PHP uses with numerous examples to show you howthese expressions can make your life easier. In “Date Handling,” we explainthe different functions used to parse and format date and time strings. In“Graphics Manipulation with GD,” we show you through two real-life scenar-ios the basic functions of creating and manipulating graphics with PHP. Thelast section in this chapter, “Multibyte Strings and Character Sets,” explainsthe different character sets and the functions to convert and handle differentones, including multi-byte strings used in Asian languages.Chapter 10, “Using PEAR,” introduces PEAR, the PHP Extension andApplication Repository. Starting with concepts and installation, the chaptershows how to use PEAR and maintain the local installed packages. This chap-ter also includes a tour of the PEAR web site.Chapter 11, “Important PEAR Packages,” gives an overview of the mostimportant PEAR packages, along with examples. Packages covered includeTemplate Systems, the Auth package to do authentication, form handlingwith the HTML_QuickForm package, and a package used to simplify caching.Chapter 12, “Building PEAR Components,” explains how to create yourown PEAR package. The PEAR Coding Standard and package.xml package def-inition format, together with tips on including files and package layout, getyou on your way to completing your first PEAR package.Chapter 13, “Making the Move,” deals with the few backward-incompatiblechanges that were introduced between PHP 4 and PHP 5. This chapter tells youwhich things you need to take care of when making your application work on PHP5, and provides workarounds wherever possible.Chapter 14, “Performance,” shows you how to make your scripts performbetter. The chapter offers tips on standard PHP usage, the use of external util-ities (APD and Xdebug) to find problems in your scripts, and PHP acceleratorslike APC and Zend Performance Suite.Gutmans_PrefaceFore Page xxvi Thursday, September 23, 2004 9:06 AM
  27. 27. A Note About Coding Styles xxviiChapter 15, “An Introduction to Writing PHP Extensions,” explains howto write your own custom PHP extension. We use a simple example to explainthe most important things like parameter parsing and resource management.Chapter 16, “PHP Shell Scripting,” shows you how to write shell scriptsin PHP, because PHP is useful for more than just web applications. We care-fully explain the differences between the CLI and CGI executables in whichPHP comes, including command-line parameter parsing and process control.This book also includes three appendices. Appendix A, “PEAR and PECLPackage Index,” provides an overview of all important packages, with descrip-tions and dependencies on other packages. Appendix B, “phpDocument FormatReference,” explains the syntax as understood by the PHP Documenter tool togenerate API documentation from source code. Appendix C, “Zend StudioQuick Start,” is an introduction to working in the Zend Studio IDE.A NOTE ABOUT CODING STYLESThere are almost as many coding styles as there are programmers. The PHPexamples in this book follow the PEAR coding standard, with the openingcurly bracket on the line below the function name. In some cases, we’ve placedthe curly bracket on the same line as the function name. We encourage you toadopt the style you are most comfortable with.Note: A code continuation character, ➥, appears at the beginning of codelines that have wrapped down from the line above it.ABOUT THE SOFTWAREIncluded in the back of this book is a special link to Zend.com, where you candownload a fully functional, 90-day trial version of the Zend Studio IDE. Besure to use the license key printed on the inside back cover of this book whenyou install Zend Studio.The Zend Development Environment (ZDE) is a convenient tool thatintegrates an editor, debugger, and project manager to help you develop, man-age, and debug your code. It can connect to your own installed server ordirectly to the Zend Studio server component. It is a powerful tool that allowsyou to debug your code in its natural environment.UPDATES AND ERRATA AND DOWNLOADSUpdates, errata, and copies of the sample programs used in this book can befound at the following URL: http//php5powerprogramming.com. We encourageyou to visit this site.Gutmans_PrefaceFore Page xxvii Thursday, September 23, 2004 9:06 AM
  28. 28. xxviii PrefaceACKNOWLEDGEMENTSThis book could not have been written without feedback from our technicalreviewers; therefore, we would like to thank Marcus Börger, Steph Fox, MartinJansen, and Rob Richards for their excellent comments and feedback. Besidesthese four reviewers, there are a few more people who helped answer severalquestions during the writing of this book, more specifically Christian Stocker forhelping with the XML chapter, Wez Furlong and Sara Golemon for answeringquestions about the streams layer, Pierre-Alain Joye for providing some insightsin the inner workings of the GD library, and less specifically the PEAR commu-nity for their support and dedication to a great repository of usable PEAR com-ponents. Some sections in this book were contributed by co-authors; GeorgRichter contributed the MySQLi section of the database chapter, and ZeevSuraski added the section on Zends Performance Suite.We would also like to thank Mark L. Taub and the editorial team of Pear-son PTR for the things they are good at doing: organizing, planning, and mar-keting this book, and making sure everything fits together. Thanks to JanetValade, for helpful developmental editing support, and our project editorKristy Hart, who helped us wrap up the book under pressure and put the finaltouches on it.Enjoy!Andi, Stig, and DerickGutmans_PrefaceFore Page xxviii Thursday, September 23, 2004 9:06 AM
  29. 29. 1C H A P T E R 1What Is New in PHP 5?“The best way to be ready for the future is to invent it.”— John Sculley1.1 INTRODUCTIONOnly time will tell if the PHP 5 release will be as successful as its two prede-cessors (PHP 3 and PHP 4). The new features and changes aim to rid PHP ofany weaknesses it may have had and make sure that it stays in the lead as theworld’s best web-scripting language.This book details PHP 5 and its new features. However, if you are familiarwith PHP 4 and are eager to know what is new in PHP 5, this chapter is for you.When you finish reading this chapter, you will have learned☞ The new language features☞ News concerning PHP extensions☞ Other noteworthy changes to PHP’s latest version1.2 LANGUAGE FEATURES1.2.1 New Object-Oriented ModelWhen Zeev Suraski added the object-oriented syntax back in the days of PHP3, it was added as “syntactic sugar for accessing collections.” The OO modelalso had support for inheritance and allowed a class (and object) to aggregateboth methods and properties, but not much more. When Zeev and Andi Gut-mans rewrote the scripting engine for PHP 4, it was a completely new engine;it ran much faster, was more stable, and boasted more features. However, theOO model first introduced in PHP 3 was barely touched.Although the object model had serious limitations, it was used exten-sively around the world, often in large PHP applications. This impressive useof the OOP paradigm with PHP 4, despite its weaknesses, led to it being themain focus for the PHP 5 release.Gutmans_Ch01 Page 1 Thursday, September 23, 2004 2:35 PM
  30. 30. 2 What Is New in PHP 5? Chap. 1So, what were some of the limitations in PHP 3 and 4? The biggest limi-tation (which led to further limitations) was the fact that the copy semantics ofobjects were the same as for native types. So, how did this actually affect thePHP developer? When assigning a variable (that points to an object) toanother variable, a copy of the object would be created. Not only did thisimpact performance, but it also usually led to obscure behavior and bugs inPHP 4 applications because many developers thought that both variableswould point at the same object, which was not the case. The variables wereinstead pointing at separate copies of the same object. Changing one wouldnot change the other.For example:class Person {var $name;function getName(){return $this->name;}function setName($name){$this->name = $name;}function Person($name){$this->setName($name);}}function changeName($person, $name){$person->setName($name);}$person = new Person("Andi");changeName($person, "Stig");print $person->getName();In PHP 4, this code would print out "Andi". The reason is that we passthe object $person to the changeName() function by-value, and thus, $person iscopied and changeName() works on a copy of $person.This behavior is not intuitive, as many developers would expect the Java-like behavior. In Java, variables actually hold a handle (or pointer) to theobject, and therefore, when it is copied, only the handle (and not the entireobject) is duplicated.There were two kinds of users in PHP 4: the ones who were aware of thisproblem and the ones who were not. The latter would usually not notice thisproblem and their code was written in a way where it did not really matter ifthe problem existed. Surely some of these people had sleepless nights trying totrack down weird bugs that they could not pinpoint. The former group dealtwith this problem by always passing and assigning objects by reference. Thiswould prevent the engine from copying their objects, but it would be a head-ache because the code included numerous & signs.Gutmans_Ch01 Page 2 Thursday, September 23, 2004 2:35 PM
  31. 31. 1.2 Language Features 3The old object model not only led to the afore-mentioned problems, butalso to fundamental problems that prevented implementing some additionalfeatures on top of the existing object model.In PHP 5, the infrastructure of the object model was rewritten to workwith object handles. Unless you explicitly clone an object by using the clonekeyword, you never create behind-the-scenes duplicates of your objects. InPHP 5, you don’t need a need to pass objects by reference or assign them byreference.Note: Passing by reference and assigning by reference are still sup-ported, in case you want to actually change a variable’s content (whetherobject or other type).1.2.2 New Object-Oriented FeaturesThe new OO features are too numerous to give a detailed description in thissection. Chapter 3, “PHP 5 OO Language,” details each feature.The following list provides the main new features:☞ public/private/protected access modifiers for methods and properties.Allows the use of common OO access modifiers to control access tomethods and properties:class MyClass {private $id = 18;public function getId() {return $this->id;}}☞ Unified constructor name __construct().Instead of the constructor being the name of the class, it is now declaredas __construct(), which makes it easier to shift classes inside class hier-archies:class MyClass {function __construct() {print "Inside constructor";}}☞ Object destructor support by defining a __destructor() method.Allows defining a destructor function that runs when an objectis destroyed:class MyClass {function __destruct() {print ”Destroying object”;}}Gutmans_Ch01 Page 3 Thursday, September 23, 2004 2:35 PM
  32. 32. 4 What Is New in PHP 5? Chap. 1☞ Interfaces.Gives the ability for a class to fulfill more than one is-a relationships. A class caninherit only from one class, but may implement as many interfaces as it wants:interface Display {function display();}class Circle implements Display {function display() {print "Displaying circlen";}}☞ instanceof operator.Language-level support for is-a relationship checking. The PHP 4 is_a() functionis now deprecated:if ($obj instanceof Circle) {print $obj is a Circle;}☞ Final methods.The final keyword allows you to mark methods so that an inheriting class cannot overloadthem:class MyClass {final function getBaseClassName() {return __CLASS__;}}☞ Final classes.After declaring a class as final, it cannot be inherited. The following examplewould error out.final class FinalClass {}class BogusClass extends FinalClass {}☞ Explicit object cloning.To clone an object, you must use the clone keyword. You may declare a __clone()method, which will be called during the clone process (after the properties havebeen copied from the original object):Gutmans_Ch01 Page 4 Thursday, September 23, 2004 2:35 PM
  33. 33. 1.2 Language Features 5class MyClass {function __clone() {print "Object is being cloned";}}$obj = new MyClass();$obj_copy = clone $obj;☞ Class constants.Class definitions can now include constant values and are referencedusing the class:class MyClass {const SUCCESS = "Success";const FAILURE = "Failure";}print MyClass::SUCCESS;☞ Static methods.You can now define methods as static by allowing them to be called fromnon-object context. Static methods do not define the $this variablebecause they are not bound to any specific object:class MyClass {static function helloWorld() {print "Hello, world";}}MyClass::helloWorld();☞ Static members.Class definitions can now include static members (properties) that areaccessible via the class. Common usage of static members is in theSingleton pattern:class Singleton {static private $instance = NULL;private function __construct() {}static public function getInstance() {if (self::$instance == NULL) {self::$instance = new Singleton();}return self::$instance;}}Gutmans_Ch01 Page 5 Thursday, September 23, 2004 2:35 PM
  34. 34. 6 What Is New in PHP 5? Chap. 1☞ Abstract classes.A class may be declared abstract to prevent it from being instantiated.However, you may inherit from an abstract class:abstract class MyBaseClass {function display() {print "Default display routine being called";}}☞ Abstract methods.A method may be declared abstract, thereby deferring its definition to aninheriting class. A class that includes abstract methods must be declaredabstract:abstract class MyBaseClass {abstract function display();}☞ Class type hints.Function declarations may include class type hints for their parameters.If the functions are called with an incorrect class type, an error occurs:function expectsMyClass(MyClass $obj) {}☞ Support for dereferencing objects that are returned from methods.In PHP 4, you could not directly dereference objects that were returnedfrom methods. You had to first assign the object to a dummy variable andthen dereference it.PHP 4:$dummy = $obj->method();$dummy->method2();PHP 5:$obj->method()->method2();☞ Iterators.PHP 5 allows both PHP classes and PHP extension classes to implementan Iterator interface. After you implement this interface, you can iterateinstances of the class by using the foreach() languageconstruct:$obj = new MyIteratorImplementation();foreach ($obj as $value) {print "$value";}Gutmans_Ch01 Page 6 Thursday, September 23, 2004 2:35 PM
  35. 35. 1.2 Language Features 7For a more complete example, see Chapter 4, “PHP 5 Advanced OOP andDesign Patterns.”☞ __autoload().Many developers writing object-oriented applications create one PHPsource file per class definition. One of the biggest annoyances is having towrite a long list of needed inclusions at the beginning of each script (one foreach class). In PHP 5, this is no longer necessary. You may define an__autoload() function that is automatically called in case you are trying to usea class that has not been defined yet. By calling this function, the scriptingengine offers one last chance to load the class before PHP bails out with anerror:function __autoload($class_name) {include_once($class_name . "php");}$obj = new MyClass1();$obj2 = new MyClass2();1.2.3 Other New Language Features☞ Exception handling.PHP 5 adds the ability for the well-known try/throw/catch structuredexception-handling paradigm. You are only allowed to throw objects thatinherit from the Exception class:class SQLException extends Exception {public $problem;function __construct($problem) {$this->problem = $problem;}}try {...throw new SQLException("Couldnt connect to database");...} catch (SQLException $e) {print "Caught an SQLException with problem $obj->problem";} catch (Exception $e) {print "Caught unrecognized exception";}Currently for backward-compatibility purposes, most internal functionsdo not throw exceptions. However, new extensions make use of this capability,and you can use it in your own source code. Also, similar to the already exist-ing set_error_handler(), you may use set_exception_handler() to catch anunhandled exception before the script terminates.Gutmans_Ch01 Page 7 Thursday, September 23, 2004 2:35 PM
  36. 36. 8 What Is New in PHP 5? Chap. 1☞ foreach with references.In PHP 4, you could not iterate through an array and modify its values.PHP 5 supports this by enabling you to mark the foreach() loop with the& (reference) sign, which makes any values you change affect the arrayover which you are iterating:foreach ($array as &$value) {if ($value === "NULL") {$value = NULL;}}☞ Default values for by-reference parameters.In PHP 4, default values could be given only to parameters, which arepassed by-values. PHP 5 now supports giving default values to by-reference parameters:function my_func(&$arg = null) {if ($arg === NULL) {print $arg is empty;}}my_func();1.3 GENERAL PHP CHANGES1.3.1 XML and Web ServicesFollowing the changes in the language, the XML updates in PHP 5 are proba-bly the most significant and exciting. The enhanced XML functionality in PHP5 puts it on par with other web technologies in some areas and overtakes themin others.1.3.1.1 The Foundation XML support in PHP 4 was implemented using avariety of underlying XML libraries. SAX support was implemented using theold Expat library, XSLT was implemented using the Sablotron library (or usinglibxml2 via the DOM extension), and DOM was implemented using the morepowerful libxml2 library by the GNOME project.Using a variety of libraries did not make PHP 4 excel when it came toXML support. Maintenance was poor, new XML standards were not alwayssupported, performance was not as good as it could have been, and interopera-bility between the various XML extensions did not exist.In PHP 5, all XML extensions have been rewritten to use the superblibxml2 XML toolkit (http://www.xmlsoft.org/). It is a feature-rich, highly main-tained, and efficient implementation of the XML standards that brings cutting-edge XML technology to PHP.Gutmans_Ch01 Page 8 Thursday, September 23, 2004 2:35 PM
  37. 37. 1.3 General PHP Changes 9All the afore-mentioned extensions (SAX, DOM, and XSLT) now uselibxml2, including the new additional extensions SimpleXML and SOAP.1.3.1.2 SAX As previously mentioned, the new SAX implementation hasswitched from using Expat to libxml2. Although the new extension should becompatible, some small subtle differences might exist. Developers who stillwant to work with the Expat library can do so by configuring and buildingPHP accordingly (which is not recommended).1.3.1.3 DOM Although DOM support in PHP 4 was also based on the libxml2library, it had bugs, memory leaks, and in many cases, the API was not W3C-compliant. The DOM extension went through a thorough facelift for PHP 5. Notonly was the extension mostly rewritten, but now, it is also W3C-compliant. Forexample, function names now use studlyCaps as described by the W3C standard,which makes it easier to read general W3C documentation and implement whatyou have learned right away in PHP. In addition, the DOM extension now sup-ports three kinds of schemas for XML validation: DTD, XML schema, andRelaxNG.As a result of these changes, PHP 4 code using DOM will not always runin PHP 5. However, in most cases, adjusting the function names to the newstandard will probably do the trick.1.3.1.4 XSLT In PHP 4, two extensions supported XSL Transformations: theSablotron extension and the XSLT support in the DOM extension. PHP 5 fea-tures a new XSL extension and, as previously mentioned, it is based on thelibxml2 extension. As in PHP 5, the XSL Transformation does not take theXSLT stylesheet as a parameter, but depends on the DOM extension to load it.The stylesheet can be cached in memory and may be applied to many docu-ments, which saves execution time.1.3.1.5 SimpleXML When looking back in a year or two, it will be clear thatSimpleXML revolutionized the way PHP developers work with XML files.Instead of having to deal with DOM or—even worse—SAX, SimpleXML repre-sents your XML file as a native PHP object. You can read, write, or iterate overyour XML file with ease, accessing elements and attributes.Consider the following XML file:<clients><client><name>John Doe</name><account_number>87234838</account_number></client><client><name>Janet Smith</name><account_number>72384329</account_number>Gutmans_Ch01 Page 9 Thursday, September 23, 2004 2:35 PM
  38. 38. 10 What Is New in PHP 5? Chap. 1</client></clients>The following code prints each client’s name and account number:$clients = simplexml_load_file(clients.xml);foreach ($clients->client as $client) {print "$client->name has account number $client➥>account_numbern";}It is obvious how simple SimpleXML really is.In case you need to implement an advanced technique in your Sim-pleXML object that is not supported in this lightweight extension, you canconvert it to a DOM tree by calling it dom_import_simplexml(), manipulate it inDOM, and convert it to SimpleXML using simplexml_import_dom().Thanks to both extensions using the same underlying XML library,switching between them is now a reality.1.3.1.6 SOAP PHP 4 lacked official native SOAP support. The most com-monly used SOAP implementation was PEARs, but because it was imple-mented entirely in PHP, it could not perform as well as a built-in C extension.Other available C extensions never reached stability and wide adoption and,therefore, were not included in the main PHP 5 distribution.SOAP support in PHP 5 was completely rewritten as a C extension and,although it was only completed at a very late stage in the beta process, it wasincorporated into the default distribution because of its thorough implementa-tion of most of the SOAP standard.The following calls SomeFunction() defined in a WSDL file:$client = new SoapClient("some.wsdl");$client->SomeFunction($a, $b, $c);1.3.1.7 New MySQLi (MySQL Improved) Extension For PHP 5, MySQL AB(http://www.mysql.com) has written a new MySQL extension that enables youto take full advantage of the new functionality in MySQL 4.1 and later. Asopposed to the old MySQL extension, the new one gives you both a functionaland an OO interface so that you can choose what you prefer. New features sup-ported by this extension include prepared statements and variable binding,SSL and compressed connections, transaction control, replication support, andmore.1.3.1.8 SQLite Extension Support for SQLite (http://www.sqlite.org) wasfirst introduced in the PHP 4.3.x series. It is an embedded SQL library thatdoes not require an SQL server, so it is suitable for applications that do notrequire the scalability of SQL servers or, if you deploy at an ISP that does notGutmans_Ch01 Page 10 Thursday, September 23, 2004 2:35 PM
  39. 39. 1.4 Other New Features in PHP 5 11offer access to an SQL server. Contrary to what its name implies, SQLite hasmany features and supports transactions, sub-selects, views, and large data-base files. It is mentioned here as a PHP 5 feature because it was introducedso late in the PHP 4 series, and because it takes advantage of PHP 5 by pro-viding an OO interface and supporting iterators.1.3.1.9 Tidy Extension PHP 5 includes support for the useful Tidy (http://tidy.sf.net/) library. It enables PHP developers to parse, diagnose, clean, andrepair HTML documents. The Tidy extension supports both a functional andan OO interface, and its API uses the PHP 5 exception mechanism.1.3.1.10 Perl Extension Although not bundled in the default PHP 5 package,the Perl extension allows you to call Perl scripts, use Perl objects, and useother Perl functionality natively from within PHP. This new extension sitswithin the PECL (PHP Extension Community Library) repository at http://pecl.php.net/package/perl.1.4 OTHER NEW FEATURES IN PHP 5This section discusses new features introduced in PHP 5.1.4.1 New Memory ManagerThe Zend Engine features a new memory manager. The two main advantagesare better support for multi-threaded environments (allocations do not need toperform any mutual exclusion locks), and after each request, freeing the allo-cated memory blocks is more efficient. Because this is an underlying infra-structure change, you will not notice it directly as the end user.1.4.2 Dropped Support for Windows 95Running PHP on the Windows 95 platform is not supported anymore due toWindows 95 does not support the functionality that PHP uses. BecauseMicrosoft officially stopped supporting it in 2002, the PHP development com-munity decided that dropping the support was a wise decision.1.5 SUMMARYYou must surely be impressed by the amount of improvements in PHP 5. Aspreviously mentioned, this chapter does not cover all the improvements, butonly the main ones. Other improvements include additional features, manybug fixes, and a much-improved infrastructure. The following chapters coverPHP 5 and give you in-depth coverage of the named new features and othersthat were not mentioned in this chapter.Gutmans_Ch01 Page 11 Thursday, September 23, 2004 2:35 PM
  40. 40. Gutmans_Ch01 Page 12 Thursday, September 23, 2004 2:35 PM
  41. 41. 13C H A P T E R 2PHP 5 Basic Language“A language that doesn’t have everything is actually easier toprogram in than some that do.”—Dennis M. Ritchie2.1 INTRODUCTIONPHP borrows a bit of its syntax from other languages such as C, shell, Perl,and even Java. It is really a hybrid language, taking the best features fromother languages and creating an easy-to-use and powerful scripting language.When you finish reading this chapter, you will have learned☞ The basic language structure of PHP☞ How PHP is embedded in HTML☞ How to write comments☞ Managing variables and basic data types☞ Defining constants for simple values☞ The most common control structures, most of which are available inother programming languages☞ Built-in or user-defined functionsIf you are an experienced PHP 4 developer, you might want to skip to thenext chapter, which covers object-oriented support of the language that haschanged significantly in PHP 5.Gutmans_ch02 Page 13 Thursday, September 23, 2004 2:37 PM

×