Table Of ContentMulti-Tier Application Programming
with PHP
Practical Guide for Architects and Programmers
The Morgan Kaufmaiin Practical Guides Series
Series Editor. Michael J. Donahoo
Multi-Tier Application Programming with PHP:
Practical Guide for Architects and Programmers
David Wall
TCP/IP Sockets in C#; Practical Guide for Programmers
David Makofske, Michael J. Donahoo, and Kenneth L. Calvert
Java Cryptography Extensions: Practical Guide for Programmers
Jason Weiss
JSP: Practical Guide for Java Programmers
Robert J. Brunner
JSTL: Practical Guide for JSP Programmers
Sue Spielman
Java: Practical Guide for Programmers
Zbigniew M. Sikora
The Struts Framework: Practical Guide for Java Programmers
Sue Spielman
Multicast Sockets: Practical Guide for Programmers
David Makofske and Kevin Almeroth
TCP/IP Sockets in Java: Practical Guide for Programmers
Kenneth L. Calvert and Michael J. Donahoo
TCP/IP Sockets in C: Practical Guide for Programmers
Michael J. Donahoo and Kenneth L. Calvert
JDEC: Practical Guide for Java Programmers
Gregory D. Speegle
For further information on these books and for a list of forthcoming titles, please visit our
website at http://www.mkp.com/practical
Multi-Tier Application
Programming with PHP
Practical Guide for Architects
and Programmers
David Wall
_ _i®
AMSTERDAM • BOSTON • HEIDELBERG • LONDON : < ^'
NEW YORK • OXFORD • PARIS • SAN DIEGO
•DT CTT» rwTUTt ^^'^ FRANCISCO • SINGAPORE • SYDNEY • TOKYO
J:ll^ri>Vl£SK Morgan Kaufmann Publishers Is an imprint of Elsevier MORGAN KAUFMANN PUBLISHERS
Senior Editor: Rick Adams
Publishing Services Manager: Andre Cuello
Associate Editor: Karyn Johnson
Project Manager: Kristin Macek
Design, illustration, and composition: Cepha Imaging Private Limited
Cover Design: Yvo Riezebos Design
Cover Image: Cargo containers being stacked at terminal.
© Sandra Baker/The Image Bank
Copyeditor: Graphic World Publishing Services
Proofreader: Graphic World Publishing Services
Printer: The Maple-Vail Book Manufacturing Group
Morgan Kaufmann Publishers is an Imprint of Elsevier.
500 Sansome Street, Suite 400, San Francisco, CA 94111
This book is printed on acid-free paper.
© 2004 by Elsevier, Inc. All rights reserved.
Designations used by companies to distinguish their products are often claimed as trademarks or
registered trademarks. In all instances in which Morgan Kaufmann Publishers is aware of a claim,
the product names appear in initial capital or all capital letters. Readers, however, should contact
the appropriate companies for more complete information regarding trademarks and registration.
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, or otherwise—without written
permission of the publishers.
Permissions may be sought directly from Elsevier's Science & Technology Rights
Department in Oxford, UK: phone: (+44) 1865 843830, fax: (+44) 1865 853333, e-mail:
permissions@elsevier.com.uk. You may also complete your request on-line via the Elsevier
homepage (http://elsevier.com) by selecting "Customer Support" and then "Obtaining Permissions.'
Library of Congress Cataloging-in-Publication Data
Application Submitted
ISBN: 0-12-732350-3
For information on all Morgan Kaufmann publications,
visit our website at www.mkp.com
Printed in the United States of America
08 07 06 05 04 5 4 3 2 1
Pour ma chere Catou. Tu es ma lumiere.
This Page Intentionally Left Blank
Contents
Preface xiii
1 Introduction 1
1.1 Defining Multi-Tier Software Design 1
1.2 Advantages of a Multi-Tier System 2
1.2.1 Software Modularity 3
1.2.2 Reliability 4
1.2.3 Division of Responsibility and Ease of Management
1.2.4 Ease of Documentation 4
1.2.5 Security 5
1.2.6 Performance and Scalability 5
1.3 Disadvantages of a Multi-Tier System 6
1.3.1 Execution Speed 6
1.3.2 Network Latency 6
1.3.3 Security 7
1.4 Questions and Exercises 7
Principles of Object Orientation in PHP 9
2.1 Creating Classes 10
2.1.1 Declaring a Class 10
2.1.2 Adding a Constructor 10
2.1.3 Adding Methods and Properties (a.k.a. Functions and
Variables) 11
VII
VIII Contents
2.2 Using a Class 12
2.2.1 Instantiating a Class 12
2.2.2 Accessing Variables in a Class Instance 12
2.2.3 Accessing Methods in a Class Instance 12
2.3 More Advanced Aspects of PHP Object Orientation 13
2.3.1 Getting a Class to Refer to Itself 13
2.3.2 Inheritance 14
2.3.3 Public, Private, and Protected Members 15
2.3.4 Overriding Class Methods 17
2.4 Questions and Exercises 19
HTTP in PHP 21
3.1 Understanding HTTP 22
3.1.1 A Simple Page Request and Response 22
3.1.2 A More Complex GET Request 24
3.1.3 A POST Request 26
3.2 Working with HTTP in PHP 26
3.2.1 Accessing GET Variables 26
3.2.2 Accessing POST Variables 27
3.2.3 Accessing Various HTML Form Elements 29
3.3 Cookies 37
3.3.1 Setting Cookies 38
3.3.2 Retrieving Cookies 39
3.3.3 Deleting Cookies 40
3.4 Sessions 40
3.4.1 Preparing the PHP Server for Session Tracking 41
3.4.2 Establishing a Session 41
3.4.3 Setting a Session Variable 42
3.4.4 Retrieving a Session Variable 42
3.5 Questions and Exercises 43
Simple Object Access Protocol Under PHP 45
4.1 Understanding SOAP 46
4.1.1 A SOAP Request 47
4.1.2 A SOAP Response 48
4.2 Implementing SOAP in PHP 50
4.2.1 A Simple Application of NuSOAP 50
4.2.2 A More Complex Application of NuSOAP 52
4.3 Questions and Exercises 53
• Contents IX
Designing and Implementing a Multi-Tier Application in PHP 55
5.1 Examining the Problem 55
5.1.1 Sketching Out the Layers 56
5.1.2 Communication Between the Layers 57
5.2 The Database Layer 58
5.2.1 Creating the Table 58
5.2.2 Populating the Table 59
5.3 The Accessor Layer 61
5.3.1 Isolating the Database Details 61
5.3.2 Getting City Names from the Database 62
5.3.3 Getting Latitudes and Longitudes from the Database 64
5.4 The Business Logic Layer 66
5.5 The Presentation Layer 68
5.6 Questions and Exercises 72
6 The Persistence Layer 73
6.1 Choosing a Persistent Storage Mechanism 73
6.1.1 Choosing a Database Server 74
6.1.2 Selecting Development Tools 75
6.2 Designing the Database 76
6.2.1 General Database Design Principles 76
6.2.2 Specific Design Requirements 76
6.3 Understanding Table Relationships and Normalization 11
6.3.1 First Normal Form 78
6.3.2 Further Normal Forms 80
6.4 Deciding on a Table Schema 80
6.4.1 Initial Table Specifications 81
6.4.2 Further Table Specifications 83
6.5 Translating the Schema into SQL 83
6.5.1 Creating the ACCT^account Table 85
6.5.2 Creating the ACCT_trans_type Table 85
6.5.3 Creating the ACCT_acct-type Table 86
6.5.4 Creating the ACCT_currency Table 86
6.5.5 Creating the ACCT.payee Table %1
6.5.6 Creating the ACCTJnstitution Table 87
6.5.7 Creating the ACCT_bank_account Table 88
6.5.8 Creating the ACCT_register Table 89
6.6 Populating the Tables 92
6.7 Questions and Exercises 93
Description:While many architects use PHP for projects, they are often not aware of the power of PHP in creating enterprise-level applications. This book covers the latest version of PHP - version 5 -- and focuses on its capabilities within a multi-tier application framework. It contains numerous coding samples