PHP 5 Power Programming Gutmans_Frontmatter Page i Thursday, September 23, 2004 9:05 AM B RUCE P ERENS ’ O PEN S OURCE S ERIES http://www.phptr.com/perens ◆ Java Application Development on Linux Carl Albing and Michael Schwarz ◆ C++ GUI Programming with Qt 3 Jasmin Blanchette, Mark Summerfield ◆ Managing Linux Systems with Webmin: System Administration and Module Development Jamie Cameron ◆ Understanding the Linux Virtual Memory Manager Mel Gorman ◆ Implementing CIFS: The Common Internet File System Christopher Hertel ◆ Embedded Software Development with eCos Anthony Massa ◆ Rapid Application Development with Mozilla Nigel McFarlane ◆ The Linux Development Platform: Configuring, Using, and Maintaining a Complete Programming Environment Rafeeq Ur Rehman, Christopher Paul ◆ Intrusion Detection with SNORT: Advanced IDS Techniques Using SNORT, Apache, MySQL, PHP, and ACID Rafeeq Ur Rehman ◆ The Official Samba-3 HOWTO and Reference Guide John H. Terpstra, Jelmer R. Vernooij, Editors ◆ Samba-3 by Example: Practical Exercises to Successful Deployment John H. Terpstra perens_series_7x9.25.fm Page 1 Wednesday, September 15, 2004 10:54 AM Gutmans_Frontmatter Page ii Thursday, September 23, 2004 9:05 AM PRENTICE HALL Professional Technical Reference Indianapolis, IN 46240 www.phptr.com PHP 5 Power Programming Andi Gutmans, Stig Sæther Bakken, and Derick Rethans Gutmans_Frontmatter Page iii Thursday, September 23, 2004 9:05 AM The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for inciden- tal or consequential damages in connection with or arising out of the use of the information or programs con- tained herein. Publisher: John Wait Editor in Chief: Don O’Hagan Acquisitions Editor: Mark L. Taub Editorial Assistant: Noreen Regina Development Editor: Janet Valade Marketing Manager: Robin O'Brien Cover Designer: Nina Scuderi Managing Editor: Gina Kanouse Senior Project Editor: Kristy Hart Copy Editor: Specialized Composition Indexer: Lisa Stumpf Senior Compositor: Gloria Schurick Manufacturing Buyer: Dan Uhrig The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U. S. Corporate and Government Sales (800) 382-3419 c orpsales@pearsontechgroup.com For sales outside the U. S., please contact: International Sales i nternational@pearsoned.com Visit us on the Web: www.phptr.com Library of Congress Cataloging-in-Publication Data: 2004107331 Copyright © 2005 Pearson Education, Inc. This material may be distrubuted only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). Pearson Education, Inc. One Lake Street Upper Saddle River, NJ 07458 Every effort was made to contact and credit all copyright holders. Use of material without proper credit is unintentional. ISBN 0-131-47149-X Text 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 To Ifat, my wife and best friend, who has patiently put up with my involement in PHP from the very beginning, and has encouraged and supported me every step of the way. Andi Gutmans To Marianne, for patience and encouragement. Stig Sæther Bakken To my parents, who care for me even when I’m not around; and to 42, the answer to life, the universe of everything. Derick Rethans Gutmans_Frontmatter Page v Thursday, September 23, 2004 9:05 AM Gutmans_Frontmatter Page vi Thursday, September 23, 2004 9:05 AM vii Contents Foreword by Zeev Suraski Preface: Introduction and Background Chapter 1: What Is New in PHP 5? Chapter 2: PHP 5 Basic Language Chapter 3: PHP 5 OO Language Chapter 4: PHP 5 Advanced OOP and Design Patterns Chapter 5: How to Write a Web Application with PHP Chapter 6: Databases with PHP 5 Chapter 7: Error Handling Chapter 8: XML with PHP 5 Chapter 9: Mainstream Extensions Chapter 10: Using PEAR Chapter 11: Important PEAR Packages Chapter 12: Building PEAR Components Chapter 13: Making the Move Chapter 14: Performance Chapter 15: An Introduction to Writing PHP Extensions Chapter 16: PHP Shell Scripting A. PEAR and PECL Package Index B. phpDocumentor Format Reference C. Zend Studio Quick Start Index Gutmans_Frontmatter Page vii Thursday, September 23, 2004 9:05 AM Gutmans_Frontmatter Page viii Thursday, September 23, 2004 9:05 AM ix Contents Foreword xxi Preface xxii 1 What Is New in PHP 5? 1 1.1 Introduction 1 1.2 Language Features 1 1.2.1 New Object-Oriented Model 1 1.2.2 New Object-Oriented Features 3 1.2.3 Other New Language Features 7 1.3 General PHP Changes 8 1.3.1 XML and Web Services 8 1.4 Other New Features in PHP 5 11 1.4.1 New Memory Manager 11 1.4.2 Dropped Support for Windows 95 11 1.5 Summary 11 2 PHP 5 Basic Language 13 2.1 Introduction 13 2.2 HTML Embedding 14 2.3 Comments 14 2.4 Variables 15 2.4.1 Indirect References to Variables 16 2.4.2 Managing Variables 16 2.4.3 Superglobals 18 2.5 Basic Data Types 18 2.5.1 Integers 19 2.5.2 Floating-Point Numbers 19 2.5.3 Strings 19 2.5.4 Booleans 22 2.5.5 Null 23 Gutmans_TOC Page ix Thursday, September 23, 2004 9:06 AM x Contents 2.5.6 Resources 23 2.5.7 Arrays 23 2.5.8 Constants 30 2.6 Operators 31 2.6.1 Binary Operators 32 2.6.2 Assignment Operators 32 2.6.3 Comparison Operators 33 2.6.4 Logical Operators 34 2.6.5 Bitwise Operators 35 2.6.6 Unary Operators 36 2.6.7 Negation Operators 36 2.6.8 Increment/Decrement Operators 37 2.6.9 The Cast Operators 38 2.6.10 The Silence Operator 39 2.6.11 The One and Only Ternary Operator 39 2.7 Control Structures 39 2.7.1 Conditional Control Structures 39 2.7.2 Loop Control Structures 42 2.7.3 Code Inclusion Control Structures 45 2.8 Functions 48 2.8.1 User-Defined Functions 49 2.8.2 Function Scope 49 2.8.3 Returning Values By Value 50 2.8.4 Returning Values By Reference 51 2.8.5 Declaring Function Parameters 52 2.8.6 Static Variables 53 2.9 Summary 54 3 PHP 5 OO Language 55 3.1 Introduction 55 3.2 Objects 55 3.3 Declaring a Class 57 3.4 The new Keyword and Constructors 57 3.5 Destructors 58 3.6 Accessing Methods and Properties Using the $this Variable 59 3.6.1 public , protected , and private Properties 60 3.6.2 public , protected , and private Methods 61 3.6.3 Static Properties 62 3.6.4 Static Methods 64 3.7 Class Constants 65 3.8 Cloning Objects 66 3.9 Polymorphism 67 3.10 parent :: and self :: 70 3.11 instanceof Operator 71 Gutmans_TOC Page x Thursday, September 23, 2004 9:06 AM [...]... PHP 5?” PHP 5 is expected to maintain and even increase PHP’s leadership in the web development market Not only does it revolutionizes PHP’s objectoriented support but it also contains many new features which make it the ultimate web development platform The rewritten XML functionality in PHP 5 puts it on par with other web technologies in some areas and overtakes them in others, especially due to the... 5, “How to Write a Web Application with PHP,” introduces you to the world of writing web applications The authors show you basics, such as handling input through form variables and safety techniques, but this chapter also includes more advanced topics, such as handling sessions with cookies and PHP's session extension You also find a few tips on laying out your source code for your web applications Chapter... .653 Index 655 Gutmans_PrefaceFore Page xxi Thursday, September 23, 2004 9:06 AM Foreword Within the last few years, PHP has grown to be the most widespread web platform in the world, operational in more than a third of the web servers across the globe PHP's growth is not only quantitative but also qualitative More and more companies, including Fortune companies, rely on PHP to run their... than that of PHP 3, with only a small amount of backward compatibility breakage4 Among other improvements was an improved extension API for better run-time performance, a web server abstraction layer allowing PHP 4 to run on most popular web servers, and lots more PHP 4 was officially released on May 22, 2002, and today its installed base has surpassed 15 million domains 2 3 http://groups.google.com/groups?selm=Dn1JM9.61t%40gpu.utcc.utoronto.ca... = null) { if ($arg === NULL) { print '$arg is empty'; } } my_func(); 1.3 GENERAL PHP CHANGES 1.3.1 XML and Web Services Following the changes in the language, the XML updates in PHP 5 are probably the most significant and exciting The enhanced XML functionality in PHP 5 puts it on par with other web technologies in some areas and overtakes them in others 1.3.1.1 The Foundation XML support in PHP 4 was... 103 4.5.2 Reflection API 103 4.5.3 Reflection Examples 106 4.5.4 Implementing the Delegation Pattern Using Reflection 107 4.6 Summary 109 5 How to Write a Web Application with PHP .111 5.1 Introduction 111 5.2 Embedding into HTML 112 5.3 User Input 114 5.4 Safe-Handling User Input 117 5.4.1 Common Mistakes... the move to PHP 5 Although Chapter 2, “PHP 5 Basic Language,” contains an introduction to PHP 5 syntax, it is meant as a refresher for PHP programmers and not as a tutorial for new programmers However, web developers with experience programming other high-level languages may indeed find that this tutorial is all they need in order to begin working effectively with PHP 5 CHAPTER OVERVIEW Chapter 1, “What... efficiently, using standard object-oriented methodologies New XML support makes PHP the best language available for processing XML and, coupled with new SOAP support, an ideal platform for creating and using Web Services This book, written by my colleague, Andi Gutmans, and two very prominent PHP developers, Stig Bakken and Derick Rethans, holds the key to unlocking the riches of PHP 5 It thoroughly covers... PHP's native functions or using PEAR DB Gutmans_PrefaceFore Page xxvi Thursday, September 23, 2004 9:06 AM xxvi Preface All scripts can throw errors, but of course you do not want them to show up 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... not be missed Chapter 8, “XML with PHP 5,” talks about the different strategies of parsing XML and converting XML to other formats with XSLT XML-RPC and SOAP are introduced to show you how to implement web services with both techniques Although not specifically for PHP 5, the five mainstream extensions that Chapter 9,“Mainstream Extensions,” covers are important enough to deserve a place in this book The . C++ GUI Programming with Qt 3 Jasmin Blanchette, Mark Summerfield ◆ Managing Linux Systems with Webmin: System Administration and Module Development Jamie Cameron ◆ Understanding the Linux. outside the U. S., please contact: International Sales i nternational@pearsoned.com Visit us on the Web: www.phptr.com Library of Congress Cataloging-in-Publication Data: 2004107331 Copyright © 2005. Language Chapter 4: PHP 5 Advanced OOP and Design Patterns Chapter 5: How to Write a Web Application with PHP Chapter 6: Databases with PHP 5 Chapter 7: Error Handling Chapter