Professional LAMP : Linux, Apache, Mysql, and PHP Web Development / Jason Gerner
Total Page:16
File Type:pdf, Size:1020Kb
01_59723x ffirs.qxd 10/31/05 6:37 PM Page iii Professional LAMP Linux®, Apache, MySQL®, and PHP5 Web Development Jason Gerner Elizabeth Naramore Morgan L. Owens Matt Warden 01_59723x ffirs.qxd 10/31/05 6:37 PM Page i Professional LAMP 01_59723x ffirs.qxd 10/31/05 6:37 PM Page ii 01_59723x ffirs.qxd 10/31/05 6:37 PM Page iii Professional LAMP Linux®, Apache, MySQL®, and PHP5 Web Development Jason Gerner Elizabeth Naramore Morgan L. Owens Matt Warden 01_59723x ffirs.qxd 10/31/05 6:37 PM Page iv Professional LAMP: Linux®, Apache, MySQL®,and PHP5 Web Development Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN-13: 978-0-7645-9723-7 ISBN-10: 0-7645-9723-X Printed in the United States of America Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 1MA/RW/RR/QV/IN Library of Congress Cataloging-in-Publication Data Professional LAMP : Linux, Apache, MySQL, and PHP Web development / Jason Gerner ... [et al.]. p. cm. ISBN-13: 978-0-7645-9723-7 (paper/website) ISBN-10: 0-7645-9723-X (paper/website) 1. Web site development. 2. Open source software. I. Title: Linux, Apache, MySQL, and PHP Web development. II. Gerner, Jason, 1978– TK5105.888.P677 2006 005.2'762—dc22 2005026487 No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355 or online at http://www.wiley.com/go/permissions. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTA- TIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMO- TIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN REN- DERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUB- LISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGA- NIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMA- TION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DIS- APPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Linux is a registered trademark of Linus Torvalds. MySQL is a registered trademark of MySQL AB A Company. All other trademarks are the property of their respec- tive owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. 01_59723x ffirs.qxd 10/31/05 6:37 PM Page v About the Authors Jason Gerner currently spends his days working as a web developer in Cincinnati and burns free time complaining about lack of support for web standards and abusing XML. He can often be found lurking in the PHPBuilder.com discussion forums, where he is a moderator. Elizabeth Naramore earned her B.S. in Organizational Behavior from Miami University (Ohio) and has been developing websites since 1997. Her main focus has been on PHP/MySQL, e-commerce, and freelance writing and teaching. Her writing can be seen in PHPBuilder.com and International PHP Magazine. She is a proud member of OINK-PUG (Ohio, Indiana, Northern Kentucky PHP Users Group) and she lives in Cincinnati, Ohio with her husband and two small children. After graduating from the University of Auckland with a mathematics degree no one was interested in, Morgan L. Owens knocked around the IT industry, becoming a backend web developer and program- mer for web-based applications for both intranet and Internet environments. He still lives in Auckland but suggests that for the right career he might—might—consider moving. Matt Warden has been developing web-based applications for over six years. His work has primarily focused on designing and implementing LAMP applications for other businesses to use internally to increase productivity. Recently, Matt has been using AJAX to offer a web-based alternative to productiv- ity gains typically only available with native desktop solutions. Currently, Matt is a lead developer at Signal US Communications in Cincinnati, Ohio. 01_59723x ffirs.qxd 10/31/05 6:37 PM Page vi 01_59723x ffirs.qxd 10/31/05 6:37 PM Page vii Credits Executive Editor Project Coordinator Carol Long Kristie Rees Senior Acquisitions Editor Graphics and Production Specialists Debra Williams Cauley Carrie Foster Lauren Goddard Development Editor Denny Hager Brian MacDonald Barbara Moore Alicia B. South Contributors William Barnett Quality Control Technicians Wm. Christopher Mastin Amanda Briggs John Greenough Technical Editors Jessica Kramer Jason Gerner Joe Niesen Elizabeth Naramore Media Development Specialists Production Editor Angie Denny Kenyon Brown Kate Jenkins Steve Kudirka Copy Editor Kit Malone Kathryn Duggan Travis Silvers Editorial Manager Media Development Coordinator Mary Beth Wakefield Laura Atkinson Production Manager Proofreading and Indexing Tim Tate TECHBOOKS Production Services Vice President and Executive Group Publisher Richard Swadley Vice President and Executive Publisher Joseph B. Wikert 01_59723x ffirs.qxd 10/31/05 6:37 PM Page viii 01_59723x ffirs.qxd 10/31/05 6:37 PM Page ix Acknowledgments Thanks to my lovely wife for putting up with me for the past few years, and being so supportive. I’d also like to thank my co-authors for their great work and the time they spent working on this project. Also, thanks to the folks at Wiley for giving us a chance, and big thanks to Brian for his huge help and having faith in us.—Jason Gerner I’d like to thank my parents, who always made me feel cool, even though I am and always will be a geek. I’d also like to thank my husband and kids for taking care of me when I’m stressed out, and for being my comic relief. Thanks also to Jason and Brian for bringing our book in for a landing.—Elizabeth Naramore I would like to thank Scott and the rest of the crew from HIT Studios not least for introducing me to this “PHP” thingy; my fellow authors for inviting me on this gig even after they saw my writing; Karl Gustafson of the University of Colorado for a piece of advice years ago, though he no doubt has no idea what I’m talking about; and my friends and family who really are a life support system—especially my grandmother Doris Madeline Rachael Shipton (1918–2005).—Morgan L. Owens Special thanks go to Sam Foster, who worked with me via email when we were initially trying to determine best practices with XMLHTTP and XMLHttpRequest object use, during a time when there was very little documentation on the subject. His ideas inspired many aspects of the design of the RemoteConnection object.—Matt Warden 01_59723x ffirs.qxd 10/31/05 6:37 PM Page x 02_59723x ftoc.qxd 10/31/05 6:35 PM Page xi Contents Acknowledgments ix Introduction xix Chapter 1: What’s New in PHP5? 1 Object-Oriented Changes 1 Passing Objects 1 Exceptions 1 Interfaces 5 Iterators 5 Constructors and Destructors 6 Access Modifiers 6 The final Keyword 6 The static Keyword 7 The abstract Keyword 7 Built-In Method Overloading Functions 8 New Functions 9 Other Changes to PHP5 12 Configuration Changes 12 MySQLi 12 XML Support 16 Tidy Extension 17 SQLite 17 Summary 17 Chapter 2: PHP5 OOP 19 Procedural Programming versus OOP 19 Basic Class Definitions 20 Visibility 23 Constructors and Destructors 25 Static Keyword 27 Class Constants 28 Assignment versus Cloning 29 02_59723x ftoc.qxd 10/31/05 6:35 PM Page xii Contents Inheritance and Interfaces 30 Inheritance 31 Interfaces 35 Magic Methods 37 __call 38 __get and __set 38 __sleep 39 __wakeup 40 __toString 43 Summary 45 Chapter 3: More Obscure PHP 47 Array Functions and Callbacks 48 Using Callbacks 48 array_map() 50 array_walk() 51 array_filter() and preg_grep() 53 preg_replace_callback() 53 call_user_func_array() and call_user_func() 55 create_function() 57 Final