PHP Advanced and Object-Oriented Programming
Visual Quickpro Guide
Larry Ullman
Visual QuickPro Guide
PHP Advanced and Object-Oriented Programming
Larry Ullman
Peachpit Press
1249 Eighth Street
Berkeley, CA 94710
Find us on the Web at: www.peachpit.com
To report errors, please send a note to:
Peachpit Press is a division of Pearson Education.
Copyright 2013 by Larry Ullman
Acquisitions Editor: Rebecca Gulick
Production Coordinator: Myrna Vladic
Copy Editor: Liz Welch
Technical Reviewer: Alan Solis
Compositor: Danielle Foster
Proofreader: Patricia Pane
Indexer: Valerie Haynes Perry
Cover Design: RHDG / Riezebos Holzbaur Design Group, Peachpit Press
Interior Design: Peachpit Press
Logo Design: MINE www.minesf.com
Notice of Rights
All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact .
Notice of Liability
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of the book, neither the author nor Peachpit Press 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 instructions contained in this book or by the computer software and hardware products described in it.
Trademarks
Visual QuickPro Guide is a registered trademark of Peachpit Press, a division of Pearson Education. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Peachpit was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book.
13-digit ISBN: 978-0-321-83218-4
10-digit ISBN: 0-321-83218-3
9 8 7 6 5 4 3 2 1
Printed and bound in the United States of America
Dedication
To my good friend Michael K. and his family: I cannot thank you all enough for your continuing friendship, generosity, and kindness over these many years.
My utmost thanks to...
Jessica, the love of my life, for just about everything.
Zoe and Sam, for making my world a better place.
Everyone at Peachpit Press for their support, for their dedication to putting out quality books, and for everything else they do to make all this happen.
The most excellent editor, Rebecca Gulick, for so many reasons.
Liz Welch, for her spot-on copyediting and attention to detail.
The production coordinator, Myrna Vladic, the compositor, Danielle Foster, the proofreader, Patricia Pane, and the indexer, Valerie Haynes Perry, who turn my mess of files into an actual book.
Alan Solis, for his very, very helpful technical review.
Thomas Larsson, for his input on the design patterns chapter. Always helpful to get even one more extra set of eyes!
Tjobbe Andrews (http://tawd.co.uk), for volunteering to create a new HTML5 design for the example chapter. And for doing so on such short notice!
Sara, for entertaining the kids so that I can get some work done, even if Id rather not.
The readers, the readers, the readers!
Table of Contents
Introduction
In this humble authors (or not-so-humble authors) opinion, advanced PHP is about continuing to learn: you already know how to use PHP, and presumably MySQL, for all the standard stuff, and now its time to expand that knowledge. This new knowledge can range from how to do different things, how to improve on the basic things, and how other technologies intersect with PHP. In short, you know how to make a dynamic Web site with PHP, but youd like to know how to make a better Web site, with every possible meaning of better.
This is the approach Ive taken in writing this book. I havent set out to blow your mind discussing esoteric idiosyncrasies the language has; rewriting the PHP, MySQL, or Apache source code; or making theoretically interesting but practically useless code. In short, I present to you several hundred pages of beyond-the-norm but still absolutely necessary (and often cool) tips and techniques.
About This Book
Simply put, Ive tried to make this books content accessible and useful for every PHP intermediate-level programmer out there. As I suggest in the introductory paragraphs, I believe that advanced PHP is mostly a matter of extended topics. You already possess all the basic knowledgeyou retrieve database query results in your sleepbut want to go further. This may mean learning object-oriented programming (OOP), using PEAR (PHP Extension and Application Repository), invoking PHP on the command line, picking up eXtensible Markup Language (XML), or fine-tuning aspects of your existing skill set.
My definition of advanced PHP programming covers three loosely grouped areas:
Doing what you already do better, faster, and more securely
Learning OOP
Doing standard things using PHP and other technologies (like networking, unit testing, or XML)
This book can be loosely divided into three sections. The first three chapters cover advanced PHP knowledge in general: programming techniques, Web applications, and databases. Those chapters all cover information that the average PHP programmer may not be familiar with but should be able to comprehend. In the process, youll pick up lots of useful code, too.
The next six chapters focus on object-oriented programming. This section constitutes about half of the book. OOP is explained starting with the fundamentals, then going into lots of advanced topics, and ending with plenty of real-world examples.
The final five chapters are all PHP and... chapters:
Communicating with networked servers
Communicating with the host server
Using the command-line interface
XML
Debugging, testing, and performance
Most examples used in this book are intended to be applicable in the real world, omitting the frivolous code you might see in other books, tutorials, and manuals. I focus almost equally on the philosophies involved as on the coding itself so that, in the end, you will come away with not just how to do this or that but also how to apply the new skills and ideas to your own projects.
Unlike with most of my other books, I do not expect that youll necessarily read this book in sequential order, for the most part. Some chapters do assume that youve read others, like the object-oriented ones, which have a progression to them. Some later chapters also reference examples completed in earlier ones. If you read the later ones first, youll just need to skip back over to the earlier ones to generate whatever database or scripts the later chapter requires.
Finally, Ill be using HTML5 in my scripts instead of HTML. Ill also use some CSS, as warranted. I do not discuss either of these subjects in this book (and, to be frank, may not adhere to them perfectly). If you are not already familiar with the subjects, you should look at some online resources or good books (such as Elizabeth Castros excellent Visual QuickStart Guides) for more information.