PHP Objects, Patterns and Practice (eBook)
XX, 536 Seiten
Apress (Verlag)
978-1-4302-2926-1 (ISBN)
This book takes you beyond the PHP basics to the enterprise development practices used by professional programmers. Updated for PHP 5.3 with new sections on closures, namespaces, and continuous integration, this edition will teach you about object features such as abstract classes, reflection, interfaces, and error handling. You'll also discover object tools to help you learn more about your classes, objects, and methods.
Then you'll move into design patterns and the principles that make patterns powerful. You'll learn both classic design patterns and enterprise and database patterns with easy-to-follow examples.
Finally, you'll discover how to put it all into practice to help turn great code into successful projects. You'll learn how to manage multiple developers with Subversion, and how to build and install using Phing and PEAR. You'll also learn strategies for automated testing and building, including continuous integration.
Taken together, these three elements-object fundamentals, design principles, and best practices-will help you develop elegant and rock-solid systems.
Matt Zandstra has worked as a web programmer, consultant, and writer for nearly two decades. He is the author of SAMS Teach Yourself PHP in 24 Hours (three editions) and a contributor to DHTML Unleashed. He has written articles for Linux Magazine, Zend.com, IBM DeveloperWorks, and php|architect Magazine, among others. Matt works as a consultant advising companies on their architectures and system management, and also develops systems primarily with PHP, and Java. Matt also writes fiction.
This book takes you beyond the PHP basics to the enterprise development practices used by professional programmers. Updated for PHP 5.3 with new sections on closures, namespaces, and continuous integration, this edition will teach you about object features such as abstract classes, reflection, interfaces, and error handling. You'll also discover object tools to help you learn more about your classes, objects, and methods. Then you'll move into design patterns and the principles that make patterns powerful. You'll learn both classic design patterns and enterprise and database patterns with easy-to-follow examples. Finally, you'll discover how to put it all into practice to help turn great code into successful projects. You'll learn how to manage multiple developers with Subversion, and how to build and install using Phing and PEAR. You'll also learn strategies for automated testing and building, including continuous integration. Taken together, these three elements-object fundamentals, design principles, and best practices-will help you develop elegant and rock-solid systems.
Matt Zandstra has worked as a web programmer, consultant, and writer for nearly two decades. He is the author of SAMS Teach Yourself PHP in 24 Hours (three editions) and a contributor to DHTML Unleashed. He has written articles for Linux Magazine, Zend.com, IBM DeveloperWorks, and php|architect Magazine, among others. Matt works as a consultant advising companies on their architectures and system management, and also develops systems primarily with PHP, and Java. Matt also writes fiction.
Title Page 1
Copyright Page 2
Contents at a Glance 3
Table of Contents 4
About the Author 16
About the Technical Reviewer 17
Acknowledgments 18
Introduction to the Third Edition 19
Part 1: Introduction 20
Chapter 1 PHP: Design and Management 21
The Problem 21
PHP and Other Languages 22
About This Book 23
Objects 24
Patterns 24
Practice 24
What’s New in the Third Edition 25
Summary 25
Part 2: Objects 27
Chapter 2 PHP and Objects 28
The Accidental Success of PHP Objects 28
In the Beginning: PHP/FI 28
Syntactic Sugar: PHP 3 28
PHP 4 and the Quiet Revolution 29
Change Embraced: PHP 5 30
Into the Future 31
Advocacy and Agnosticism: The Object Debate 31
Summary 31
Chapter 3: Object Basics 32
Classes and Objects 32
A First Class 32
A First Object (or Two) 33
Setting Properties in a Class 34
Working with Methods 36
Creating a Constructor Method 38
Arguments and Types 39
Primitive Types 39
Primitive Types Matter: An Example 40
Taking the Hint: Object Types 42
Inheritance 44
The Inheritance Problem 44
Working with Inheritance 48
Constructors and Inheritance 50
Invoking an Overridden Method 52
Public, Private, and Protected: Managing Access to Your Classes 52
Accessor Methods 54
The ShopProduct Classes 54
Summary 56
Chapter 4 Advanced Features 57
Static Methods and Properties 57
Constant Properties 60
Abstract Classes 61
Interfaces 63
Late Static Bindings: The static Keyword 64
Handling Errors 67
Exceptions 68
Throwing an Exception 69
Subclassing Exception 70
Final Classes and Methods 73
Working with Interceptors 74
Defining Destructor Methods 78
Copying Objects with __clone() 79
Defining String Values for Your Objects 81
Callbacks, Anonymous Functions and Closures 82
Summary 86
Chapter 5 Object Tools 87
PHP and Packages 87
PHP Packages and Namespaces 87
Namespaces to the Rescue 88
Using the File System to Simulate Packages 92
Naming the PEAR Way 94
Include Paths 94
Autoload 96
The Class and Object Functions 97
Looking for Classes 98
Learning About an Object or Class 99
Learning About Methods 100
Learning About Properties 101
Learning About Inheritance 101
Method Invocation 102
The Reflection API 103
Getting Started 103
Time to Roll Up Your Sleeves 104
Examining a Class 106
Examining Methods 107
Examining Method Arguments 109
Using the Reflection API 110
Summary 113
Chapter 6 Objects and Design 114
Defining Code Design 114
Object-Oriented and Procedural Programming 115
Responsibility 118
Cohesion 119
Coupling 119
Orthogonality 119
Choosing Your Classes 120
Polymorphism 121
Encapsulation 122
Forget How to Do It 123
Four Signposts 124
Code Duplication 124
The Class Who Knew Too Much 124
The Jack of All Trades 124
Conditional Statements 125
The UML 125
Class Diagrams 125
Representing Classes 125
Attributes 126
Operations 127
Describing Inheritance and Implementation 127
Associations 128
Aggregation and Composition 129
Describing Use 130
Using Notes 131
Sequence Diagrams 132
Summary 134
Part 3: Patterns 135
Chapter 7 What Are Design Patterns? Why Use Them? 136
What Are Design Patterns? 136
A Design Pattern Overview 138
Name 138
The Problem 138
The Solution 139
Consequences 139
The Gang of Four Format 139
Why Use Design Patterns? 140
A Design Pattern Defines a Problem 140
A Design Pattern Defines a Solution 140
Design Patterns Are Language Independent 140
Patterns Define a Vocabulary 140
Patterns Are Tried and Tested 141
Patterns Are Designed for Collaboration 141
Design Patterns Promote Good Design 141
PHP and Design Patterns 142
Summary 142
Chapter 8 Some Pattern Principles 143
The Pattern Revelation 143
Composition and Inheritance 144
The Problem 144
Using Composition 147
Decoupling 149
The Problem 149
Loosening Your Coupling 151
Code to an Interface, Not to an Implementation 153
The Concept That Varies 154
Patternitis 155
The Patterns 155
Patterns for Generating Objects 155
Patterns for Organizing Objects and Classes 155
Task-Oriented Patterns 155
Enterprise Patterns 156
Database Patterns 156
Summary 156
Chapter 9 Generating Objects 157
Problems and Solutions in Generating Objects 157
The Singleton Pattern 161
The Problem 161
Implementation 162
Consequences 164
Factory Method Pattern 164
The Problem 165
Implementation 167
Consequences 169
Abstract Factory Pattern 169
The Problem 170
Implementation 171
Consequences 173
Prototype 174
The Problem 175
Implementation 175
But That’s Cheating! 178
Summary 179
Chapter 10 Patterns for Flexible Object Programming 180
Structuring Classes to Allow Flexible Objects 180
The Composite Pattern 180
The Problem 181
Implementation 183
Consequences 186
Composite in Summary 189
The Decorator Pattern 190
The Problem 190
Implementation 192
Consequences 196
The Facade Pattern 196
The Problem 196
Implementation 197
Consequences 198
Summary 198
Chapter 11 Performing and Representing Tasks 199
The Interpreter Pattern 199
The Problem 199
Implementation 200
Interpreter Issues 207
The Strategy Pattern 208
The Problem 208
Implementation 209
The Observer Pattern 212
Implementation 214
The Visitor Pattern 220
The Problem 220
Implementation 221
Visitor Issues 225
The Command Pattern 226
The Problem 226
Implementation 226
Summary 230
Chapter 12 Enterprise Patterns 231
Architecture Overview 231
The Patterns 232
Applications and Layers 232
Cheating Before We Start 235
Registry 235
The Problem 235
Implementation 236
Registry, Scope, and PHP 239
Consequences 244
The Presentation Layer 245
Front Controller 245
The Problem 245
Implementation 246
Consequences 254
Application Controller 255
The Problem 255
Implementation 256
Consequences 266
Page Controller 267
The Problem 267
Implementation 267
Consequences 271
Template View and View Helper 272
The Problem 272
Implementation 272
Consequences 274
The Business Logic Layer 274
Transaction Script 275
The Problem 275
Implementation 275
Consequences 279
Domain Model 279
The Problem 279
Implementation 280
Consequences 282
Summary 283
Chapter 13 Database Patterns 284
The Data Layer 284
Data Mapper 284
The Problem 285
Implementation 285
Handling Multiple Rows 289
Consequences 296
Identity Map 297
The Problem 297
Implementation 298
Consequences 300
Unit of Work 300
The Problem 301
Implementation 301
Consequences 305
Lazy Load 305
The Problem 305
Implementation 306
Consequences 307
Domain Object Factory 307
The Problem 307
Implementation 308
Consequences 309
The Identity Object 310
The Problem 310
Implementation 311
Consequences 316
The Selection Factory and Update Factory Patterns 316
The Problem 316
Implementation 316
Consequences 320
What’s Left of Data Mapper Now? 320
Summary 322
Part 4: Practice 324
Chapter 14 Good (and Bad) Practice 325
Beyond Code 325
Borrowing a Wheel 325
Playing Nice 327
Giving Your Code Wings 327
Documentation 328
Testing 329
Continuous Integration 330
Summary 330
Chapter 15 An Introduction to PEAR and Pyrus 331
What Is PEAR? 331
Phar Out with Pyrus 332
Installing a Package 334
PEAR Channels 335
Using a PEAR Package 337
Handling PEAR Errors 339
Creating Your Own PEAR Package 342
package.xml 342
Package Elements 342
The contents Element 344
Dependencies 347
Tweaking Installation with phprelease 348
Preparing a Package for Shipment 349
Setting Up Your Own Channel 349
Defining a Channel with PEAR2_SimpleChannelServer 350
Managing a PEAR Channel with PEAR2_SimpleChannelFrontend 351
Managing a Package 352
Summary 354
Chapter 16 Generating Documentation with phpDocumentor 355
Why Document? 355
Installation 356
Generating Documentation 357
DocBlock Comments 358
Documenting Classes 360
File-Level Documentation 361
Documenting Properties 361
Documenting Methods 363
Creating Links in Documentation 364
Summary 367
Chapter 17 Version Control with Subversion 368
Why Use Version Control? 368
Getting Subversion 369
Configuring a Subversion Repository 370
Creating a Repository 370
Beginning a Project 371
Updating and Committing 375
Adding and Removing Files and Directories 378
Adding a File 378
Removing a File 379
Adding a Directory 379
Removing Directories 380
Tagging and Exporting a Release 380
Tagging a Project 380
Exporting a Project 381
Branching a Project 381
Summary 385
Chapter 18 Testing with PHPUnit 386
Functional Tests and Unit Tests 386
Testing by Hand 387
Introducing PHPUnit 389
Creating a Test Case 389
Assertion Methods 390
Testing Exceptions 391
Running Test Suites 392
Constraints 393
Mocks and Stubs 395
Tests Succeed When They Fail 398
Writing Web Tests 401
Refactoring a Web Application for Testing 401
Simple Web Testing 404
Introducing Selenium 405
Getting Selenium 405
Creating a Test 406
A Note of Caution 410
Summary 412
Chapter 19 Automated Build with Phing 413
What Is Phing? 413
Getting and Installing Phing 414
Composing the Build Document 414
Targets 416
Properties 418
Types 422
FileSet 423
PatternSet 424
FilterChain 425
Tasks 427
Echo 427
Copy 427
Input 429
Delete 431
Summary 431
Chapter 20 Continuous Integration 432
What Is Continuous Integration? 432
Preparing a Project for CI 433
CI and Version Control 434
Unit Tests 435
Documentation 436
Code Coverage 436
Coding Standards 438
PHP Code Browser 438
Build 440
CruiseControl and phpUnderControl 441
Installing CruiseControl 441
Installing phpUnderControl 443
Installing Your Project 445
Running phpUnderControl / CruiseControl 448
Test Failures 450
Failure Notification 451
Adding Your Own Build Targets 452
Summary 455
Part 5: Conclusion 456
Chapter 21 Objects, Patterns, Practice 457
Objects 457
Choice 458
Encapsulation and Delegation 458
Decoupling 458
Reusability 459
Aesthetics 459
Patterns 459
What Patterns Buy Us 460
Tried and Tested 460
Patterns Suggest Other Patterns 460
A Common Vocabulary 460
Patterns Promote Design 460
Patterns and Principles of Design 460
Favor Composition over Inheritance 461
Avoid Tight Coupling 461
Code to an Interface, Not an Implementation 461
Encapsulate the Concept That Varies 462
Practice 462
Testing 463
Documentation 463
Version Control 463
Automated Build 463
Continuous Integration 464
What I Missed 464
Summary 464
Appendix A Bibliography 466
Books 466
Articles 467
Sites 467
Appendix B A Simple Parser 469
The Scanner 469
The Parser 476
Index 489
Erscheint lt. Verlag | 28.12.2010 |
---|---|
Zusatzinfo | XX, 536 p. |
Verlagsort | Berkeley |
Sprache | englisch |
Themenwelt | Mathematik / Informatik ► Informatik ► Theorie / Studium |
Mathematik / Informatik ► Informatik ► Web / Internet | |
Schlagworte | Control • Database • Management • PHP • PHP 5 • programming |
ISBN-10 | 1-4302-2926-8 / 1430229268 |
ISBN-13 | 978-1-4302-2926-1 / 9781430229261 |
Haben Sie eine Frage zum Produkt? |
Größe: 6,0 MB
DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasserzeichen und ist damit für Sie personalisiert. Bei einer missbräuchlichen Weitergabe des eBooks an Dritte ist eine Rückverfolgung an die Quelle möglich.
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.
Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich