Tài liệu PHP Object - Oriented Solutions P1 doc

30 610 1
Tài liệu PHP Object - Oriented Solutions P1 doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

this print for reference only—size & color not accurate spine = 0.911" 392 page count DAVID POWERS In this book you’ll learn how to: Produce code that’s easier to maintain by adopting OOP techniques. Use best practices by implementing basic design patterns. Simplify complex code through encapsulation. Unlock the secrets of the Standard PHP Library. Generate your own news feed using Really Simple Syndication (RSS). P HP is easy to learn and a great way to add dynamic functionality to web sites, such as sending email from online forms and generating database-driven content. But there soon comes a time when you realize you’re writing similar scripts over and over again. By adopting an object-oriented approach, you can avoid the need to reinvent the wheel every time, creating scripts that are reusable, easier to understand, and easier to maintain. The main barrier to object-oriented programming (OOP) is that it’s based on unfamiliar concepts, such as objects, classes, interfaces, encapsulation, and polymorphism. This book strips away the mystique and explains each concept in an approachable and understandable way. It provides a gentle but fast-paced introduction to OOP as it applies to PHP. Another barrier to the adoption of OOP among PHP developers has been the slow migra- tion from PHP 4, which used a radically different—and inferior—object model. Now that PHP 4 has come to the official end of its life, this book concentrates exclusively on using OOP with PHP 5 and 6. So you can be confi- dent that you’re learning skills that won’t be out of date almost as soon as you put the book down. Through a series of practical projects, this book shows how OOP can be used to group related functions in a portable manner. The projects include a validator for fil- tering user input, a class that avoids the need to remember all the esoteric PHP date formatting codes, and an XML generator—everyday requirements for a lot of develop- ers. Although the emphasis is on learning how the code works, if you’re in a hurry, the PHP classes used in the book can be downloaded from the friends of ED web site and incorporated into existing sites with a minimum of effort. The book is aimed at intermediate developers with a good understanding of PHP basics, such as variables, arrays, functions, loops, and conditional statements. It provides the necessary groundwork for advancing on to using an object-oriented framework, such as the Zend Framework, and taking your PHP coding skills to the next level. Powers CYAN YELLOW MAGENTA BLACK PHP OBJECT-ORIENTED SOLUTIONS Understand basic OOP concepts, such as inheritance, encapsulation, and polymorphism. Extend core PHP classes. Design and create your own classes for PHP 5 and 6. Also Available US $36.99 Mac/PC compatible www.friendsofed.com http://foundationphp.com/ SHELVI NG CATE GOR Y 1. PHP Available from Apress ISBN 978-1-4302-1011-5 9 781430 210115 5 3 6 9 9 PHP Object-Oriented Solutions David Powers 10115fm.qxd 7/22/08 12:10 PM Page i PHP Object-Oriented Solutions Copyright © 2008 by David Powers A ll rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. I SBN-13 (pbk): 978-1-4302-1011-5 I SBN-13 (electronic): 978-1-4302-1012-2 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark o wner, with no intention of infringement of the trademark. Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or v isit w ww.springeronline.com . For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales—eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is freely available to readers at www.friendsofed.com in the Downloads section. Credits Lead Editor Ben Renow-Clarke Technical Reviewer Seungyeob Choi Editorial Board Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank P ohlmann, Ben R enow-Clarke, Dominic Shakeshaft , Matt Wade, Tom Welsh Project Manager Beth Christmas Copy Editors Heather Lang and Damon Larson Associate Production Director Kari Brooks-Copony Production Editor Laura Esterman Compositor Molly Sharp Proofreader Patrick Vincent Indexer Toma Mulligan Artist April Milne Interior and Cover Designer Kurt Krames Manufacturing Director Tom Debolski 10115fm.qxd 7/22/08 12:10 PM Page ii CONTENTS AT A GLANCE About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Chapter 1: Why Object-Oriented PHP? . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: Writing PHP Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Chapter 3: Taking the Pain Out of Working with Dates . . . . . . . . 77 Chapter 4: Using PHP Filters to Validate User Input . . . . . . . . . . 121 Chapter 5: Building a Versatile Remote File Connector . . . . . . . 169 Chapter 6: SimpleXML—Couldn’t Be Simpler . . . . . . . . . . . . . . . 207 Chapter 7: Supercharged Looping with SPL . . . . . . . . . . . . . . . . . 251 Chapter 8: Generating XML from a Database . . . . . . . . . . . . . . . 289 Chapter 9: Case Study: Creating Your Own RSS Feed . . . . . . . . . 321 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 10115fm.qxd 7/22/08 12:10 PM Page iii 10115fm.qxd 7/22/08 12:10 PM Page iv CONTENTS About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Chapter 1: Why Object-Oriented PHP? . . . . . . . . . . . . . . . . . . . . . . . 3 Understanding basic OOP concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 How OOP evolved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Using classes and objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Protecting data integrity with encapsulation. . . . . . . . . . . . . . . . . . . . . . . . . 8 Polymorphism is the name of the game . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Extending classes through inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Deciding on a class hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Using best practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 How OOP has evolved in PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 OOP since PHP 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Preparing for PHP 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Choosing the right tools to work with PHP classes . . . . . . . . . . . . . . . . . . . . . . . 16 Using a specialized script editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Chapter review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Chapter 2: Writing PHP Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Formatting code for readability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Using the Zend Framework PHP Coding Standard . . . . . . . . . . . . . . . . . . . . . 25 Choosing descriptive names for clarity . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Creating classes and objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Defining a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Controlling access to properties and methods . . . . . . . . . . . . . . . . . . . . . . . 27 Quick review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Setting default values with a constructor method . . . . . . . . . . . . . . . . . . . . . 33 v 10115fm.qxd 7/22/08 12:10 PM Page v Using inheritance to extend a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 D efining a child class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 A ccessing a parent class’s methods and properties . . . . . . . . . . . . . . . . . . . . 39 Using the scope resolution operator . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Controlling changes to methods and properties . . . . . . . . . . . . . . . . . . . . . . 44 Preventing a class or method from being overridden. . . . . . . . . . . . . . . . . 44 Using class constants for properties . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Creating static properties and methods . . . . . . . . . . . . . . . . . . . . . . . . 47 Quick review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Loading classes automatically. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Exploring advanced OOP features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Creating abstract classes and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Simulating multiple inheritance with interfaces . . . . . . . . . . . . . . . . . . . . . . 54 Understanding which class an object is an instance of . . . . . . . . . . . . . . . . . . 55 Restricting acceptable data with type hinting . . . . . . . . . . . . . . . . . . . . . 56 Using magic methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Converting an object to a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Cloning an object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Accessing properties automatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Accessing methods automatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Cleaning up with a destructor method . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Handling errors with exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Throwing an exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Catching an exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Extracting information from an exception . . . . . . . . . . . . . . . . . . . . . . . . . 68 Extending the Exception class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Using comments to generate code hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Writing PHPDoc comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Chapter review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Chapter 3: Taking the Pain Out of Working with Dates . . . . . . . . 77 Designing the class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Examining the built-in date-related classes . . . . . . . . . . . . . . . . . . . . . . . . . 79 Using the DateTime class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Setting the default time zone in PHP . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Examining the DateTimeZone class . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Using the DateTimeZone class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Deciding how to extend the existing classes . . . . . . . . . . . . . . . . . . . . . . . . 89 Building the class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Creating the class file and constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 R esetting the time and date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Accepting dates in common formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Accepting a date in MM/DD/YYYY format . . . . . . . . . . . . . . . . . . . . . . . 98 Accepting a date in DD/MM/YYYY format . . . . . . . . . . . . . . . . . . . . . . . 99 Accepting a date in MySQL format . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Outputting dates in common formats . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Outputting date parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Performing date-related calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 CONTENTS vi 10115fm.qxd 7/22/08 12:10 PM Page vi Adding and subtracting days or weeks . . . . . . . . . . . . . . . . . . . . . . . . 105 A dding months . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 S ubtracting months . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Adding and subtracting years . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Calculating the number of days between two dates . . . . . . . . . . . . . . . . . 113 Creating a default date format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Creating read-only properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Organizing and commenting the class file . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Chapter review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Chapter 4: Using PHP Filters to Validate User Input . . . . . . . . . . 121 Validating input with the filter functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Understanding how the filter functions work . . . . . . . . . . . . . . . . . . . . . . . 123 filter_has_var() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 filter_list() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 filter_id(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Setting filter options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Filtering single variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Setting flags and options when filtering a single variable . . . . . . . . . . . . . . 134 Filtering multiple variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Setting a default filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Building the validation class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Deciding what the class will do. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Planning how the class will work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Coding the validation class properties and methods . . . . . . . . . . . . . . . . . . . 140 Naming properties and defining the constructor . . . . . . . . . . . . . . . . . . 140 Setting the input type and checking required fields . . . . . . . . . . . . . . . . . 142 Preventing duplicate filters from being applied to a field . . . . . . . . . . . . . . 147 Creating the validation methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Creating the methods to process the tests and get the results . . . . . . . . . . . 157 Using the validation class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Sticking to your design decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Chapter review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Chapter 5: Building a Versatile Remote File Connector . . . . . . . 169 Designing the class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Building the class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Defining the constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Checking the URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Retrieving the remote file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Defining the accessDirect() method . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Using cURL to retrieve the remote file . . . . . . . . . . . . . . . . . . . . . . . . 186 Using a socket connection to retrieve the remote file. . . . . . . . . . . . . . . . 190 Handling the response headers from a socket connection . . . . . . . . . . . . . 196 Generating error messages based on the status code . . . . . . . . . . . . . . . . 202 Final testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Ideas for improving the class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Chapter review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 CONTENTS vii 10115fm.qxd 7/22/08 12:10 PM Page vii Chapter 6: SimpleXML—Couldn’t Be Simpler . . . . . . . . . . . . . . . 207 A quick XML primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 W hat is XML?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 How XML documents are structured. . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 The rules of writing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Using HTML entities in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Inserting HTML and other code in XML . . . . . . . . . . . . . . . . . . . . . . . . 213 Using SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Loading an XML document with SimpleXML . . . . . . . . . . . . . . . . . . . . . . . 217 Loading XML from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Loading XML from a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Extracting data with SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Accessing text nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Accessing attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Accessing unknown nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Saving and modifying XML with SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Outputting and saving SimpleXMLElement objects. . . . . . . . . . . . . . . . . . . . 228 Modifying SimpleXMLElement objects. . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Changing the values of text and attributes . . . . . . . . . . . . . . . . . . . . . . 231 Removing nodes and values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Adding attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Adding new elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Using SimpleXML with namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 How namespaces are used in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Handling namespace prefixes in SimpleXML . . . . . . . . . . . . . . . . . . . . . . . 236 Handling namespaced attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Finding out which namespaces a document uses. . . . . . . . . . . . . . . . . . . . . 242 Using SimpleXML with XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 A quick introduction to XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Using XPath to drill down into XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Using XPath expressions for finer control. . . . . . . . . . . . . . . . . . . . . . . 246 Using XPath with namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Registering namespaces to work with XPath . . . . . . . . . . . . . . . . . . . . . 247 Chapter review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Chapter 7: Supercharged Looping with SPL . . . . . . . . . . . . . . . . . 251 Introducing iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Using an array with SPL iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Limiting the number of loops with the LimitIterator . . . . . . . . . . . . . . . . . . . 253 Using SimpleXML with an iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Setting options for R egexIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Looping sequentially through more than one set of data . . . . . . . . . . . . . . . . 263 Looking ahead with the CachingIterator. . . . . . . . . . . . . . . . . . . . . . . . . . 265 Using anonymous iterators as shorthand . . . . . . . . . . . . . . . . . . . . . . . . . 268 Examining files and directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Using DirectoryIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Including subdirectories in a single operation . . . . . . . . . . . . . . . . . . . . . . 271 CONTENTS viii 10115fm.qxd 7/22/08 12:10 PM Page viii [...]... T- O R I E N T E D P H P ? 10115ch01.qxd 7/10/08 1:12 PM Page 4 P H P O B J E C T- O R I E N T E D S O L U T I O N S Let’s get things straight right from the start: PHP (PHP Hypertext Preprocessor) is not an object- oriented language, but it does have extensive object- oriented features These underwent comprehensive revision and enhancement when PHP 5 was released in July 2004, and the PHP 5 object- oriented. .. 97 8-1 -5 905 9-7 3 1-6 ) The techniques and code used in this book require PHP 5 or PHP 6 They will not work with PHP 4 In this introductory chapter, you’ll learn about the following topics: How OOP evolved and the thinking behind it What an object is and how it differs from a class What terms such as encapsulation, inheritance, and polymorphism really mean How the object- oriented model has developed in PHP. .. 12:10 PM Page xi ABOUT THE AUTHOR David Powers is the author of a series of highly successful books on PHP, including PHP Solutions: Dynamic Web Design Made Easy (friends of ED, ISBN: 97 8-1 -5 905 9-7 3 1-6 ) and The Essential Guide to Dreamweaver CS3 with CSS, Ajax, and PHP (friends of ED, ISBN: 978 1-5 905 9-8 5 9-7 ) As a professional writer, he has been involved in electronic media for more than 30 years, first... for converting it to PHP 4 Even though at the time of publication, it’s estimated that more than half of all PHP- driven websites still run on PHP 4, all support for PHP 4 officially ended on August 8, 2008 PHP 4 is dead Long live PHP 5 (and PHP 6 when it’s released) If you haven’t yet made the switch from PHP 4, now is the time to do it Who should read this book If you develop in PHP, but haven’t yet... theory and explains how OOP fits into PHP; Chapter 2 then goes into the mechanics of writing objectoriented code in PHP The remaining seven chapters put all the theory into practice, showing you how to create and use your own classes and objects, as well as covering object- oriented features that have been built into core PHP since version 5 You don’t need to be a PHP expert to follow this book, but you... screen) owes its genesis to a tongue-in-cheek exchange with Steve Fleischer of Flying Tiger Web Design (www flyingtigerwebdesign.com), who suggested I should write Powers Object- Oriented PHP Actually, he phrased it rather differently If you take the initial letters of the suggested title, you’ll get the drift But Steve had an important point: he felt that books on object- oriented programming (OOP) frequently... for PHP Object- Oriented Solutions Download the ZIP file, and unzip its contents into a new folder inside your web server document root I named the folder OopSolutions, but you can call it whatever you want In addition to a series of folders named ch2_exercises through ch9_exercises, the folder should contain the following: Ch2: This contains example class definitions for use with ch2_exercises class_docs:... of the new features added to PHP 5 are object- oriented, this means you can easily extend core PHP classes to add new functionality or simply make them work the way you want them to In fact, Chapter 3 does precisely that: it extends the PHP DateTime class to make it easier to use The project in Chapter 4 takes the PHP filter functions and hides them behind a much more user-friendly interface Chapter... features added to core PHP in version 5: SimpleXML and the Standard PHP Library (SPL) The XML theme continues in the final two chapters, which use the PHP XMLWriter class to generate XML on the fly from a database and show you how to create a news feed from your site The need for OOP has come about because PHP is being used increasingly for large-scale web applications Object- oriented practices break... with a three- or four-letter prefix followed by an underscore All classes in this book will be prefixed with Pos_ (for PHP Object- Oriented Solutions) , so the class in Chapter 4 will be called Pos_Validator When you want to use any of the class’s properties or methods, you need to create an instance of the class by using the new keyword like this: $val = new Pos_Validator(); This creates an object called . ISBN 97 8-1 -4 30 2-1 01 1-5 9 781430 210115 5 3 6 9 9 PHP Object- Oriented Solutions David Powers 10115fm.qxd 7/22/08 12:10 PM Page i PHP Object- Oriented Solutions. the copyright owner and the publisher. I SBN-13 (pbk): 97 8-1 -4 30 2-1 01 1-5 I SBN-13 (electronic): 97 8-1 -4 30 2-1 01 2-2 Printed and bound in the United States

Ngày đăng: 12/12/2013, 22:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan