Over the past three years PHP has evolved from being a niche language used to add dynamic functionality to small sites to a powerful tool making strong inroads into large-scale, business-critical Web systems. The rapid maturation of PHP has created a skeptical population of users from more traditional "enterprise" languages who question the readiness and ability of PHP to scale, as well as a large population of PHP developers without formal computer science backgrounds who have learned through the hands-on experimentation while developing small and midsize applications in PHP. While there are many books on learning PHP and developing small applications with it, there is a serious lack of information on "scaling" PHP for large-scale, business-critical systems. Schlossnagle's Advanced PHP Programming fills that void, demonstrating that PHP is ready for enterprise Web applications by showing the reader how to develop PHP-based applications for maximum performance, stability, and extensibility. ISBN 9780672325618......Page 5 Advanced PHP Programming......Page 4 Contents at a Glance......Page 6 Table of Contents......Page 8 About the Author......Page 20 Reader Services......Page 21 Foreword......Page 22 PHP in the Enterprise......Page 24 This Book’s Structure and Organization......Page 25 Platforms and Versions......Page 29 PART I Implementation and Development Methodologies......Page 30 CHAPTER 1 Coding Styles......Page 32 Choosing a Style That Is Right for You......Page 33 Naming Symbols......Page 42 Avoiding Confusing Code......Page 50 Documentation......Page 52 Further Reading......Page 58 CHAPTER 2 Object-Oriented Programming Through Design Patterns......Page 60 Introduction to OO Programming......Page 61 A Brief Introduction to Design Patterns......Page 67 Overloading......Page 81 Further Reading......Page 95 CHAPTER 3 Error Handling......Page 96 Handling Errors......Page 98 Handling External Errors......Page 103 Exceptions......Page 106 When to Use Exceptions......Page 127 Further Reading......Page 128 CHAPTER 4 Implementing with PHP: Templates and the Web......Page 130 Smarty......Page 131 Writing Your Own Template Solution......Page 143 Further Reading......Page 144 CHAPTER 5 Implementing with PHP: Standalone Scripts......Page 146 Handling Input/Output (I/O)......Page 148 Parsing Command-Line Arguments......Page 151 Creating and Managing Child Processes......Page 153 Writing Daemons......Page 161 Combining What You’ve Learned: Monitoring Services......Page 164 Further Reading......Page 173 CHAPTER 6 Unit Testing......Page 176 An Introduction to Unit Testing......Page 177 Writing Inline and Out-of-Line Unit Tests......Page 180 Additional Features in PHPUnit......Page 185 Test-Driven Design......Page 191 Unit Testing in a Web Environment......Page 202 Further Reading......Page 205 CHAPTER 7 Managing the Development Environment......Page 206 Change Control......Page 207 Managing Packaging......Page 222 Further Reading......Page 229 CHAPTER 8 Designing a Good API......Page 230 Design for Refactoring and Extensibility......Page 231 Defensive Coding......Page 236 Further Reading......Page 239 PART II Caching......Page 240 Language-Level Tunings......Page 242 Cache-Friendly PHP Applications......Page 254 Content Compression......Page 258 Further Reading......Page 259 Caching Issues......Page 262 Choosing the Right Strategy: Hand-Made or Prefab Classes......Page 264 Output Buffering......Page 265 In-Memory Caching......Page 267 DBM-Based Caching......Page 274 Shared Memory Caching......Page 280 Cookie-Based Caching......Page 281 Integrating Caching into Application Code......Page 287 Further Reading......Page 304 Introduction by Example: Fibonacci Sequences......Page 306 Caching Reused Data Inside a Request......Page 312 Caching Reused Data Between Requests......Page 315 Computational Reuse Inside PHP......Page 318 Further Reading......Page 319 PART III Distributed Applications......Page 320 CHAPTER 12 Interacting with Databases......Page 322 Understanding How Databases and Queries Work......Page 323 Database Access Patterns......Page 329 Tuning Database Access......Page 340 Further Reading......Page 345 CHAPTER 13 User Authentication and Session Security......Page 346 Simple Authentication Schemes......Page 347 Registering Users......Page 350 Maintaining Authentication: Ensuring That You Are Still Talking to the Same Person......Page 354 Single Signon......Page 362 Further Reading......Page 369 CHAPTER 14 Session Handling......Page 372 Client-Side Sessions......Page 373 Server-Side Sessions......Page 377 What Is a Cluster?......Page 390 Clustering Design Essentials......Page 393 Caching in a Distributed Environment......Page 398 Scaling Databases......Page 407 Further Reading......Page 414 CHAPTER 16 RPC: Interacting with Remote Services......Page 416 XML-RPC......Page 417 SOAP......Page 426 SOAP and XML-RPC Compared......Page 436 Further Reading......Page 437 PART IV Performance......Page 440 CHAPTER 17 Application Benchmarks:Testing an Entire Application......Page 442 Passive Identification of Bottlenecks......Page 443 Load Generators......Page 445 Further Reading......Page 450 CHAPTER 18 Profiling......Page 452 A Smorgasbord of Profilers......Page 453 Installing and Using APD......Page 454 A Tracing Example......Page 456 Profiling a Larger Application......Page 458 Spotting General Inefficiencies......Page 463 Removing Superfluous Functionality......Page 465 Further Reading......Page 470 CHAPTER 19 Synthetic Benchmarks: Evaluating Code Blocks and Functions......Page 472 Benchmarking Basics......Page 473 Building a Benchmarking Harness......Page 474 Benchmarking Examples......Page 485 PART V Extensibility......Page 496 CHAPTER 20 PHP and Zend Engine Internals......Page 498 How the Zend Engine Works: Opcodes and Op Arrays......Page 499 Variables......Page 505 Functions......Page 509 Classes......Page 510 The PHP Request Life Cycle......Page 515 Further Reading......Page 525 CHAPTER 21 Extending PHP: Part I......Page 526 Extension Basics......Page 527 An Example: The Spread Client Wrapper......Page 560 Further Reading......Page 570 Implementing Classes......Page 572 Writing Custom Session Handlers......Page 587 The Streams API......Page 591 Further Reading......Page 602 SAPIs......Page 604 Modifying and Introspecting the Zend Engine......Page 621 Homework......Page 632 Index......Page 634 This is the eBook version of the printed book. If the print book includes a CD-ROM, this content is not included within the eBook version. Over the past three years PHP has evolved from being a niche language used to add dynamic functionality to small sites to a powerful tool making strong inroads into large-scale, business-critical Web systems. The rapid maturation of PHP has created a skeptical population of users from more traditional'enterprise'languages who question the readiness and ability of PHP to scale, as well as a large population of PHP developers without formal computer science backgrounds who have learned through the hands-on experimentation while developing small and midsize applications in PHP. While there are many books on learning PHP and developing small applications with it, there is a serious lack of information on'scaling'PHP for large-scale, business-critical systems. Schlossnagle's Advanced PHP Programming fills that void, demonstrating that PHP is ready for enterprise Web applications by showing the reader how to develop PHP-based applications for maximum performance, stability, and extensibility. PHP has exploded in popularity, and is now starting to make inroads into large-scale business-critical Web systems. Schlossnagle provides a definitive guide to developing PHP applications for performance, stability and extensibility