OBJEC T-ORIE NT E D
PHP
OBJEC T-ORIE NT E D
PHP
C O N C E P T S , T E C H N I Q U E S , A N D C O D E
P E T E R L A V I N
®
Trying to learn the concepts of object-oriented
programming using a language like C++ can be an
intimidating experience. The simpler object model of
PHP makes it the ideal language for an introduction to
object-oriented programming.
For the uninitiated, this guide to the latest version of
PHP offers a speedy introduction to object-oriented
programming, including a thorough explanantion of
class as well as coverage of constructors, destructors,
inheritance, polymorphism and interfaces. In this easy
to follow guide, the author teaches by example, and
couples new concepts with snippets of example code
(which are available for download from the book’s
companion website).
With Object-Oriented PHP, you’ll learn to:
Promote code reuse by creating your own classes
and using built-in classes
Customize and improve classes through inheritance
Simplify database access by developing MySQL
database and result set classes
Incorporate an RSS reader into your site using only
four lines of code
Use XML easily with an object-oriented approach
Employ PHP to autogenerate documentation for
your code
Make thumbnail images on the fly
Replace clumsy error trapping with Exception
handling
Take full advantage of advanced OO features in PHP
Object-Oriented PHP will show you how to maximize
PHP’s features, with much less work than you might think.
With Object-Oriented PHP, you can master the basics of
OOP and get up to speed on PHP in one fell swoop.
A B O U T T H E A U T H O R
Peter Lavin runs a web development firm based in
Toronto, Canada, and he writes regularly on all things
tech. He has been published in a number of magazines
and online publications, including UnixReview.com,
Spider Magazine and PlugIn Magazine. He is also a
contributor to the forthcoming PHP Hacks (O’Reilly).
ww w.nostarch.com
I LAY F LAT.
This book uses RepKover —a durable binding that won’t snap shut.
TH E F I NE ST I N G E E K EN TE RTA I N M EN T
SHELVE IN:
WEB PROGRAMMING
$39.95 ($51.95 CDN)
®
L E A R N T H E
B A S I C S O F
O B J E C T - O R I E N T E D
P R O G R A M M I N G
A N D P H P
L E A R N T H E
B A S I C S O F
O B J E C T - O R I E N T E D
P R O G R A M M I N G
A N D P H P
®
O B J E C T- O R I E N T E D P H P
O B J E C T- O R I E N T E D P H P
L A V I N
OBJECT-ORIENTED
PHP
Concepts, Techniques,
and Code
by Peter Lavin
San Francisco
®
OBJECT-ORIENTED PHP. Copyright © 2006 by Peter Lavin.
All 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.
Printed on recycled paper in the United States of America
1 2 3 4 5 6 7 8 9 10 – 09 08 07 06
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and
company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark
symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the
benefit of the trademark owner, with no intention of infringement of the trademark.
Publisher: William Pollock
Managing Editor: Elizabeth Campbell
Associate Production Editor: Christina Samuell
Cover and Interior Design: Octopod Studios
Developmental Editor: William Pollock
Technical Reviewer: Peter MacIntyre
Copyeditors: Publication Services, Inc. and Sarah Lemaire
Compositor: Riley Hoffman
Proofreader: Stephanie Provines
For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
555 De Haro Street, Suite 250, San Francisco, CA 94107
phone: 415.863.9900; fax: 415.863.9950; info@nostarch.com; www.nostarch.com
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been
taken in the preparation of this work, neither the author nor No Starch Press, Inc. 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 it.
Library of Congress Cataloging-in-Publication Data
Lavin, Peter.
Object-oriented PHP : concepts, techniques, and code / Peter Lavin.
p. cm.
Includes index.
ISBN 1-59327-077-1
1. PHP (Computer program language) 2. Object-oriented programming (Computer science) I. Title.
QA76.73.P224L38 2006
005.1'17--dc22
2006015309
No Starch Press, Copyright © 2006 by Peter Lavin
BRIEF CONTENTS
Acknowledgments ........................................................................................................ xiii
Introduction ...................................................................................................................xv
Chapter 1: What a Tangled Web We Weave....................................................................1
Chapte