1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP Master: Write Cutting-edge Code potx

404 1,3K 2

Đ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

Thông tin cơ bản

Định dạng
Số trang 404
Dung lượng 14,88 MB

Nội dung

PANTONE 2955 CPANTONE Orange 021 C CMYK 100, 45, 0, 37CMYK O, 53, 100, 0 Black 100%Black 50% CMYK: Pantone: Grey scale BY LORNA MITCHELL DAVEY SHAFIK MATTHEW TURLAND MODERN, EFFICIENT, AND SECURE TECHNIQUES FOR PHP PROFESSIONALS PHP MASTER WRITE CUTTING-EDGE CODE Summary of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix 1. Object Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2. Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3. APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 4. Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 5. Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 6. Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 7. Automated Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 8. Quality Assurance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 A. PEAR and PECL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 B. SPL: The Standard PHP Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 C. Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 PHP MASTER: WRITE CUTTING-EDGE CODE BY LORNA MITCHELL DAVEY SHAFIK MATTHEW TURLAND PHP Master: Write Cutting-edge Code by Lorna Mitchell, Davey Shafik, and Matthew Turland Copyright © 2011 SitePoint Pty. Ltd. Author Image (M. Turland): Dawn CaseyProduct Manager: Simon Mackie Author Image (L. Mitchell): Sebastian Bergmann Technical Editor: Tom Museth Expert Reviewer: Luke Cawood Indexer: Michele Combs Editor: Kelly Steele Cover Designer: Alex Walker Notice of Rights All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means without the prior written permission of the publisher, except in the case of brief quotations included in critical articles or reviews. Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors, will be held liable for any damages caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein. Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark. Published by SitePoint Pty. Ltd. 48 Cambridge Street, Collingwood VIC 3066 Australia Web: www.sitepoint.com Email: business@sitepoint.com ISBN 978-0-9870908-7-4 (print) ISBN 978-0-9871530-4-3 (ebook) Printed and bound in the United States of America iv About Lorna Mitchell Lorna Jane Mitchell is a PHP consultant based in Leeds, UK. She has a Masters in Electronic Engineering, and has worked in a variety of technical roles throughout her career. She spe- cializes in working with data and APIs. Lorna is active in the PHP community, organizing the PHP North West conference and user group, leading the Joind.in open source project, and speaking at conferences. She has been published in .net magazine and php|architect, to name a couple; she also blogs regularly on her own site, http://lornajane.net. About Davey Shafik Davey Shafik has been working with PHP and the LAMP stack, as well as HTML, CSS, and JavaScript for over a decade. With numerous books, articles, and conference appearances under his belt, he enjoys teaching others any way he can. An avid photographer, he lives in sunny Florida with his wife and six cats. About Matthew Turland Matthew Turland has been using PHP since 2002. He is a Zend Certified Engineer in PHP 5 and Zend Framework, has published articles in php|architect magazine, and contributed to two books: php|architect’s Guide to Web Scraping with PHP (Toronto: NanoBooks, 2010) and the one you’re reading now. He’s also been a speaker at php|tek, Confoo, and ZendCon. He enjoys contributing to open source PHP projects including Zend Framework, PHPUnit, and Phergie, as well as blogging on his website, http://matthewturland.com. About Luke Cawood After nearly ten years of PHP development, Luke joined the SitePoint family to work at 99designs.com, the world’s largest crowdsourced design community. Luke has a passion for web and mobile technologies, and when not coding, enjoys music festivals and all things food-related. He’s known to blog occasionally at http://lukecawood.com. About Tom Museth Tom Museth first fell in love with code while creating scrolling adventure games in BASIC on his Commodore 64, and usability testing them on reluctant family members. He then spent 16 years as a journalist and production editor before deciding web development would be more rewarding. He has a passion for jQuery, PHP, HTML5, and CSS3, is eagerly eyeing the world of mobile dev, and likes to de-stress via a book, a beach, and a fishing rod. v For Kevin, who may have taught me everything I know, and everyone else who believed I could do this. —Lorna For Grandpa Leslie, for showing me how to be a good man, and for my wife, Frances, for loving the man I became because of him. —Davey To my parents and my wife, who always encourage and believe in me. And to my children and my friends, who continue to inspire me. —Matthew Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix What’s in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Where to Find Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii The SitePoint Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii The Book’s Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii The SitePoint Newsletters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv The SitePoint Podcast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Your Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Lorna Mitchell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Davey Shafik . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Matthew Turland . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Tips, Notes, and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Chapter 1 Object Oriented Programming . . . . . . . . 1 Why OOP? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Vocabulary of OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Introduction to OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Declaring a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Class Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Instantiating an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Autoloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Using Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Using Static Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Objects and Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Object Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Objects and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Type Hinting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Objects and References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Passing Objects as Function Parameters . . . . . . . . . . . . . . . . . . . . . . 16 Fluent Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 public, private, and protected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 public . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 private . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 protected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Choosing the Right Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Using Getters and Setters to Control Visibility . . . . . . . . . . . . . . . . . 21 Using Magic __get and __set Methods . . . . . . . . . . . . . . . . . . . . . 22 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 SPL Countable Interface Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Counting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Declaring and Using an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Identifying Objects and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Why Exceptions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Extending Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Catching Specific Types of Exception . . . . . . . . . . . . . . . . . . . . . . . . 29 Setting a Global Exception Handler . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Working with Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 More Magic Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Using __call() and __callStatic() . . . . . . . . . . . . . . . . . . . . 33 x [...]... writing modern PHP, you may see this convention in legacy or PHP 4-compatible code, and PHP 5 does support them The constructor is always called when we instantiate an object, and we can use it to set up and configure the object before we release it for use in the code The constructor also has a matching magic method called a destructor, which takes the 3 4 PHP Master: Write Cutting-edge Code method name... where they wouldn’t strictly be needed 7 8 PHP Master: Write Cutting-edge Code Objects and Namespaces Since PHP 5.3, PHP has had support for namespaces There are two main aims of this new feature The first is to avoid the need for classes with names like Zend_InfoCard_Xml_Security_Transform_Exception, which at 47 characters long is inconvenient to have in code (no disrespect to Zend Framework—we just... 285 phploc 286 phpcpd 287 phpmd 288 Coding Standards 290 Checking Coding Standards with PHP Code Sniffer 290 Viewing Coding Standards Violations 293 PHP Code Sniffer Standards... example .php (excerpt) $mono = new Courier('Monospace Delivery'); If additional code is to be inserted into an existing example, the new code will be displayed in bold: function animate() { new_variable = "Hello"; } Where existing code is required for context, rather than repeat all the code, a vertical ellipsis will be displayed: function animate() { ⋮ return new_variable; } Some lines of code are... before the end of this one The Errata page3 on the book’s website will always have the latest information about known typographical and code errors 1 http://www.sitepoint.com/forums/ http://www.sitepoint.com/books/phppro /code .php 3 http://www.sitepoint.com/books/phppro/errata .php 2 xxiv The SitePoint Newsletters In addition to books like this one, SitePoint publishes free email newsletters, such as the SitePoint... object, instantiated an object, and talked about autoloading, but we’re yet to do much object oriented programming We’ll want to work with 5 6 PHP Master: Write Cutting-edge Code both properties and methods of the objects we create, so let’s see some example code for doing exactly that: $mono = new Courier('Monospace Delivery'); // accessing a property echo "Courier Name: " $mono->name; // calling... items: Code Samples Code in this book will be displayed using a fixed-width font, like so: xxvi class Courier { public function construct($name) { $this->name = $name; return true; } } If the code is to be found in the book’s code archive, the name of the file will appear at the top of the program listing, like this: example .php function autoload($classname) { include strtolower($classname) ' .php' ;... prefixes so that we know what they relate to, or stored in arrays to keep elements together, using objects allows us to collect values together, as well as add functionality to that unit 2 PHP Master: Write Cutting-edge Code Vocabulary of OOP What sometimes puts people off from working with objects is the tendency to use big words to refer to perfectly ordinary concepts So to avoid deterring you, we’ll... forward with PHP Conventions Used in This Book You’ll notice that we’ve used certain typographic and layout styles throughout the book to signify different types of information Firstly, because this is a book about PHP, we’ve dispensed with the opening and closing tags (< ?php and ?>) in most code examples and assumed you’ll have them inserted in your own files The only exception is where PHP is printed... repositories, and yet we’ve met plenty of PHP developers who are yet to use them In this appendix, we provide full instructions for setting these up, so there’s no longer an excuse for being ignorant of the jewels within Appendix B: SPL: The Standard PHP Library The Standard PHP Library is a fabulous and under-celebrated extension that ships as standard with PHP and contains some very powerful tools . . . . . . . . 359 PHP MASTER: WRITE CUTTING-EDGE CODE BY LORNA MITCHELL DAVEY SHAFIK MATTHEW TURLAND PHP Master: Write Cutting-edge Code by Lorna Mitchell,. TURLAND MODERN, EFFICIENT, AND SECURE TECHNIQUES FOR PHP PROFESSIONALS PHP MASTER WRITE CUTTING-EDGE CODE Summary of Contents Preface . . . . . . . . .

Ngày đăng: 06/03/2014, 05:21

TỪ KHÓA LIÊN QUAN