Advanced PHP Programming

Book description

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.

Table of contents

  1. Copyright
    1. Dedication
  2. About the Author
  3. Acknowledgments
  4. We Want to Hear from You!
  5. Reader Services
  6. Foreword
  7. Introduction
    1. PHP in the Enterprise
    2. This Book’s Structure and Organization
      1. Part I, “Implementation and Development Methodologies”
        1. Chapter 1, “Coding Styles”
        2. Chapter 2, “Object-Oriented Programming Through Design Patterns”
        3. Chapter 3, “Error Handling”
        4. Chapter 4, “Implementing with PHP: Templates and the Web”
        5. Chapter 5, “Implementing with PHP: Standalone Scripts”
        6. Chapter 6, “Unit Testing”
        7. Chapter 7, “Managing the Development Environment”
        8. Chapter 8, “Designing a Good API”
      2. Part II, “Caching”
        1. Chapter 9, “External Performance Tunings”
        2. Chapter 10, “Data Component Caching”
        3. Chapter 11, “Computational Reuse”
      3. Part III, “Distributed Applications”
        1. Chapter 12, “Interacting with Databases”
        2. Chapter 13, “User Authentication and Session Security”
        3. Chapter 14, “Session Handling”
        4. Chapter 15, “Building a Distributed Environment”
        5. Chapter 16, “RPC: Interacting with Remote Services”
      4. Part IV, “Performance”
        1. Chapter 17, “Application Benchmarks: Testing an Entire Application”
        2. Chapter 18, “Profiling”
        3. Chapter 19, “Synthetic Benchmarks: Evaluating Code Blocks and Functions”
      5. Part V, “Extensibility”
        1. Chapter 20, “PHP and Zend Engine Internals”
        2. Chapter 21, “Extending PHP: Part I”
        3. Chapter 22, “Extending PHP: Part II”
        4. Chapter 23, “Writing SAPIs and Extending the Zend Engine”
    3. Platforms and Versions
  8. I. Implementation and Development Methodologies
    1. 1. Coding Styles
      1. Choosing a Style That Is Right for You
      2. Code Formatting and Layout
        1. Indentation
        2. Line Length
        3. Using Whitespace
        4. SQL Guidelines
        5. Control Flow Constructs
          1. Using Braces in Control Structures
          2. Consistently Using Braces
          3. for Versus while Versus foreach
          4. Using break and continue to Control Flow in Loops
          5. Avoiding Deeply Nested Loops
      3. Naming Symbols
        1. Constants and Truly Global Variables
        2. Long-Lived Variables
        3. Temporary Variables
        4. Multiword Names
        5. Function Names
        6. Class Names
        7. Method Names
        8. Naming Consistency
        9. Matching Variable Names to Schema Names
      4. Avoiding Confusing Code
        1. Avoiding Using Short Open Tags
        2. Avoiding Using echo to Construct HTML
        3. Using Parentheses Judiciously
      5. Documentation
        1. Inline Comments
        2. API Documentation
          1. Using phpDocumentor
      6. Further Reading
    2. 2. Object-Oriented Programming Through Design Patterns
      1. Introduction to OO Programming
        1. Inheritance
        2. Encapsulation
        3. Static (or Class) Attributes and Methods
        4. Special Methods
      2. A Brief Introduction to Design Patterns
        1. The Adaptor Pattern
        2. The Template Pattern
        3. Polymorphism
        4. Interfaces and Type Hints
        5. The Factory Pattern
        6. The Singleton Pattern
      3. Overloading
        1. SPL and Iterators
        2. _ _call()
        3. _ _autoload()
      4. Further Reading
    3. 3. Error Handling
      1. Handling Errors
        1. Displaying Errors
        2. Logging Errors
        3. Ignoring Errors
        4. Acting On Errors
      2. Handling External Errors
      3. Exceptions
        1. Using Exception Hierarchies
        2. A Typed Exceptions Example
        3. Cascading Exceptions
        4. Handling Constructor Failure
        5. Installing a Top-Level Exception Handler
        6. Data Validation
      4. When to Use Exceptions
      5. Further Reading
    4. 4. Implementing with PHP: Templates and the Web
      1. Smarty
        1. Installing Smarty
        2. Your First Smarty Template: Hello World!
        3. Compiled Templates Under the Hood
        4. Smarty Control Structures
        5. Smarty Functions and More
        6. Caching with Smarty
        7. Advanced Smarty Features
      2. Writing Your Own Template Solution
      3. Further Reading
    5. 5. Implementing with PHP: Standalone Scripts
      1. Introduction to the PHP Command-Line Interface (CLI)
      2. Handling Input/Output (I/O)
      3. Parsing Command-Line Arguments
      4. Creating and Managing Child Processes
        1. Closing Shared Resources
        2. Sharing Variables
        3. Cleaning Up After Children
        4. Signals
          1. SIGCHLD
          2. SIGALRM
        5. Other Common Signals
      5. Writing Daemons
        1. Changing the Working Directory
        2. Giving Up Privileges
        3. Guaranteeing Exclusivity
      6. Combining What You’ve Learned: Monitoring Services
      7. Further Reading
    6. 6. Unit Testing
      1. An Introduction to Unit Testing
        1. Writing Unit Tests for Automated Unit Testing
        2. Writing Your First Unit Test
        3. Adding Multiple Tests
      2. Writing Inline and Out-of-Line Unit Tests
        1. Inline Packaging
        2. Separate Test Packaging
        3. Running Multiple Tests Simultaneously
      3. Additional Features in PHPUnit
        1. Creating More Informative Error Messages
        2. Adding More Test Conditions
        3. Using the setUp() and tearDown() Methods
        4. Adding Listeners
        5. Using Graphical Interfaces
      4. Test-Driven Design
        1. The Flesch Score Calculator
        2. Testing the Word Class
        3. Bug Report 1
      5. Unit Testing in a Web Environment
      6. Further Reading
    7. 7. Managing the Development Environment
      1. Change Control
        1. CVS Basics
        2. Modifying Files
        3. Examining Differences Between Files
        4. Helping Multiple Developers Work on the Same Project
        5. Symbolic Tags
        6. Branches
        7. Maintaining Development and Production Environments
      2. Managing Packaging
        1. Packaging and Pushing Code
        2. Packaging Binaries
        3. Packaging Apache
        4. Packaging PHP
      3. Further Reading
    8. 8. Designing a Good API
      1. Design for Refactoring and Extensibility
        1. Encapsulating Logic in Functions
        2. Keeping Classes and Functions Simple
        3. Namespacing
        4. Reducing Coupling
      2. Defensive Coding
        1. Establishing Standard Conventions
        2. Using Sanitization Techniques
      3. Further Reading
  9. II. Caching
    1. 9. External Performance Tunings
      1. Language-Level Tunings
        1. Compiler Caches
        2. Optimizers
        3. HTTP Accelerators
        4. Reverse Proxies
        5. Operating System Tuning for High Performance
        6. Proxy Caches
      2. Cache-Friendly PHP Applications
      3. Content Compression
      4. Further Reading
        1. RFCs
        2. Compiler Caches
        3. Proxy Caches
        4. Content Compression
    2. 10. Data Component Caching
      1. Caching Issues
      2. Recognizing Cacheable Data Components
      3. Choosing the Right Strategy: Hand-Made or Prefab Classes
      4. Output Buffering
      5. In-Memory Caching
        1. Flat-File Caches
        2. Cache Size Maintenance
        3. Cache Concurrency and Coherency
      6. DBM-Based Caching
        1. Cache Concurrency and Coherency
        2. Cache Invalidation and Management
      7. Shared Memory Caching
      8. Cookie-Based Caching
        1. Cache Size Maintenance
        2. Cache Concurrency and Coherency
      9. Integrating Caching into Application Code
        1. Caching Home Pages
        2. Using Apache’s mod_rewrite for Smarter Caching
        3. Caching Part of a Page
        4. Implementing a Query Cache
      10. Further Reading
    3. 11. Computational Reuse
      1. Introduction by Example: Fibonacci Sequences
      2. Caching Reused Data Inside a Request
      3. Caching Reused Data Between Requests
      4. Computational Reuse Inside PHP
        1. PCREs
        2. Array Counts and Lengths
      5. Further Reading
  10. III. Distributed Applications
    1. 12. Interacting with Databases
      1. Understanding How Databases and Queries Work
        1. Query Introspection with EXPLAIN
        2. Finding Queries to Profile
      2. Database Access Patterns
        1. Ad Hoc Queries
        2. The Active Record Pattern
        3. The Mapper Pattern
        4. The Integrated Mapper Pattern
      3. Tuning Database Access
        1. Limiting the Result Set
        2. Lazy Initialization
      4. Further Reading
    2. 13. User Authentication and Session Security
      1. Simple Authentication Schemes
        1. HTTP Basic Authentication
        2. Query String Munging
        3. Cookies
      2. Registering Users
        1. Protecting Passwords
        2. Protecting Passwords Against Social Engineering
      3. Maintaining Authentication: Ensuring That You Are Still Talking to the Same Person
        1. Checking That $_SERVER['REMOTE_IP'] Stays the Same
        2. Ensuring That $_SERVER['USER_AGENT'] Stays the Same
        3. Using Unencrypted Cookies
        4. Things You Should Do
          1. Using Encryption
          2. Using Expiration Logic
            1. Expiration on Every Request
            2. Expiration After a Fixed Time
          3. Collecting User Identity Information
          4. Collecting Versioning Information
          5. Logging Out
        5. A Sample Authentication Implementation
      4. Single Signon
        1. A Single Signon Implementation
      5. Further Reading
    3. 14. Session Handling
      1. Client-Side Sessions
        1. Implementing Sessions via Cookies
        2. Building a Slightly Better Mousetrap
      2. Server-Side Sessions
        1. Tracking the Session ID
          1. Native Methods for Tracking the Session ID
        2. A Brief Introduction to PHP Sessions
        3. Custom Session Handler Methods
        4. Garbage Collection
          1. Garbage Collection in the files Handler
          2. Garbage Collection in the mm Handler
          3. Garbage Collection in the MySession Handler
        5. Choosing Between Client-Side and Server-Side Sessions
          1. Implementing Native Session Handlers
    4. 15. Building a Distributed Environment
      1. What Is a Cluster?
      2. Clustering Design Essentials
        1. Planning to Fail
        2. Working and Playing Well with Others
          1. Always Namespace Your Functions
          2. Reference Services by Full Descriptive Names
          3. Namespace Your System Resources
        3. Distributing Content to Your Cluster
        4. Scaling Horizontally
        5. Specialized Clusters
      3. Caching in a Distributed Environment
        1. Centralized Caches
        2. Fully Decentralized Caches Using Spread
      4. Scaling Databases
        1. Writing Applications to Use Master/Slave Setups
        2. Alternatives to Replication
        3. Alternatives to RDBMS Systems
      5. Further Reading
    5. 16. RPC: Interacting with Remote Services
      1. XML-RPC
        1. Building a Server: Implementing the MetaWeblog API
        2. Auto-Discovery of XML-RPC Services
      2. SOAP
        1. WSDL
        2. Rewriting system.load as a SOAP Service
        3. Amazon Web Services and Complex Types
        4. Generating Proxy Code
      3. SOAP and XML-RPC Compared
      4. Further Reading
        1. SOAP
        2. XML-RPC
        3. Web Logging
        4. Publicly Available Web Services
  11. IV. Performance
    1. 17. Application Benchmarks: Testing an Entire Application
      1. Passive Identification of Bottlenecks
      2. Load Generators
        1. ab
        2. httperf
          1. The Log-Based Generator
          2. The Session Simulator
          3. The Realistic Data Generator
        3. Daiquiri
      3. Further Reading
    2. 18. Profiling
      1. What Is Needed in a PHP Profiler
      2. A Smorgasbord of Profilers
      3. Installing and Using APD
      4. A Tracing Example
      5. Profiling a Larger Application
      6. Spotting General Inefficiencies
      7. Removing Superfluous Functionality
      8. Further Reading
    3. 19. Synthetic Benchmarks: Evaluating Code Blocks and Functions
      1. Benchmarking Basics
      2. Building a Benchmarking Harness
        1. PEAR’s Benchmarking Suite
        2. Building a Testing Harness
        3. Adding Data Randomization on Every Iteration
        4. Removing Harness Overhead
        5. Adding Custom Timer Information
        6. Writing Inline Benchmarks
      3. Benchmarking Examples
        1. Matching Characters at the Beginning of a String
        2. Macro Expansions
        3. Interpolation Versus Concatenation
  12. V. Extensibility
    1. 20. PHP and Zend Engine Internals
      1. How the Zend Engine Works: Opcodes and Op Arrays
      2. Variables
      3. Functions
      4. Classes
        1. The Object Handlers
        2. Object Creation
        3. Other Important Structures
      5. The PHP Request Life Cycle
        1. The SAPI Layer
        2. The PHP Core
        3. The PHP Extension API
        4. The Zend Extension API
        5. How All the Pieces Fit Together
      6. Further Reading
    2. 21. Extending PHP: Part I
      1. Extension Basics
        1. Creating an Extension Stub
        2. Building and Enabling Extensions
        3. Using Functions
          1. A Function Example
        4. Managing Types and Memory
        5. Parsing Strings
          1. Other Return Macros
        6. Manipulating Types
        7. Type Testing Conversions and Accessors
        8. Using Resources
        9. Returning Errors
        10. Using Module Hooks
          1. Module Startup and Shutdown
            1. Defining Constants
            2. Enabling Globals
            3. Parsing INI Entries
          2. Module Shutdown
          3. Request Startup and Shutdown
            1. Request Startup
            2. Request Shutdown
          4. phpinfo() Registration
      2. An Example: The Spread Client Wrapper
        1. MINIT
        2. MSHUTDOWN
        3. Module Functions
        4. Using the Spread Module
      3. Further Reading
    3. 22. Extending PHP: Part II
      1. Implementing Classes
        1. Creating a New Class
        2. Adding Properties to a Class
        3. Class Inheritance
        4. Adding Methods to a Class
        5. Adding Constructors to a Class
        6. Throwing Exceptions
        7. Using Custom Objects and Private Variables
        8. Using Factory Methods
        9. Creating and Implementing Interfaces
      2. Writing Custom Session Handlers
      3. The Streams API
      4. Further Reading
    4. 23. Writing SAPIs and Extending the Zend Engine
      1. SAPIs
        1. The CGI SAPI
          1. The CGI SAPI Application
        2. The Embed SAPI
        3. SAPI Input Filters
      2. Modifying and Introspecting the Zend Engine
        1. Warnings as Exceptions
        2. An Opcode Dumper
        3. APD
        4. APC
        5. Using Zend Extension Callbacks
      3. Homework

Product information

  • Title: Advanced PHP Programming
  • Author(s): George Schlossnagle
  • Release date: February 2004
  • Publisher(s): Sams
  • ISBN: None