PHP 5 Power Programming - Pearsoncmg

Transcription

Gutmans Frontmatter Page i Thursday, September 23, 2004 9:05 AMPHP 5 Power Programming

perens series 7x9.25.fmGutmans FrontmatterPagePageii Thursday,1 Wednesday,SeptemberSeptember23, 200415, 20049:05 10:54AM AMBRUCE 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. Terpstra

Gutmans Frontmatter Page iii Thursday, September 23, 2004 9:05 AMPHP 5 Power ProgrammingAndi Gutmans, Stig Sæther Bakken,and Derick RethansPRENTICE HALLProfessional Technical ReferenceIndianapolis, IN 46240www.phptr.com

Gutmans Frontmatter Page iv Thursday, September 23, 2004 2:14 PMThe 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 incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.Publisher: John WaitEditor in Chief: Don O’HaganAcquisitions Editor: Mark L. TaubEditorial Assistant: Noreen ReginaDevelopment Editor:Janet ValadeMarketing Manager: Robin O'BrienCover 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 v Thursday, September 23, 2004 9:05 AMTo 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 Rethans

Gutmans Frontmatter Page vi Thursday, September 23, 2004 9:05 AM

Gutmans Frontmatter Page vii Thursday, September 23, 2004 9:05 AMviiContentsForeword by Zeev SuraskiPreface: Introduction and BackgroundChapter 1:Chapter 2:Chapter 3:Chapter 4:Chapter 5:Chapter 6:Chapter 7:Chapter 8:Chapter 9:Chapter 10:Chapter 11:Chapter 12:Chapter 13:Chapter 14:Chapter 15:Chapter 16:A.B.C.IndexWhat Is New in PHP 5?PHP 5 Basic LanguagePHP 5 OO LanguagePHP 5 Advanced OOP and Design PatternsHow to Write a Web Application with PHPDatabases with PHP 5Error HandlingXML with PHP 5Mainstream ExtensionsUsing PEARImportant PEAR PackagesBuilding PEAR ComponentsMaking the MovePerformanceAn Introduction to Writing PHP ExtensionsPHP Shell ScriptingPEAR and PECL Package IndexphpDocumentor Format ReferenceZend Studio Quick Start

Gutmans Frontmatter Page viii Thursday, September 23, 2004 9:05 AM

Gutmans TOC Page ix Thursday, September 23, 2004 9:06 AMContentsForeword. 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 . 23ix

Gutmans TOC Page x Thursday, September 23, 2004 9:06 AMxContents2.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.71

Gutmans TOC Page xi Thursday, September 23, 2004 9:06 AMContentsxi3.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 . 134

Gutmans TOC Page xii Thursday, September 23, 2004 9:06 AMxiiContents5.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 .

PRENTICE HALL Professional Technical Reference Indianapolis, IN 46240 www.phptr.com PHP 5 Power Program