You are previewing Modern PHP.
O'Reilly logo
Modern PHP

Book Description

PHP is experiencing a renaissance, though it may be difficult to tell with all of the outdated PHP tutorials online. With this practical guide, you’ll learn how PHP has become a full-featured, mature language with object-orientation, namespaces, and a growing collection of reusable component libraries. You’ll learn best practices for application architecture and planning, databases, security, testing, debugging, and deployment.

Table of Contents

  1. Preface
    1. What You Need to Know About This Book
    2. How This Book Is Organized
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments
  2. I. Language Features
  3. 1. The New PHP
    1. Past
    2. Present
    3. Future
  4. 2. Features
    1. Namespaces
      1. Why We Use Namespaces
      2. Declaration
      3. Import and Alias
      4. Helpful Tips
    2. Code to an Interface
    3. Traits
      1. Why We Use Traits
      2. How to Create a Trait
      3. How to Use a Trait
    4. Generators
      1. Create a Generator
      2. Use a Generator
    5. Closures
      1. Create
      2. Attach State
    6. Zend OPcache
      1. Enable Zend OPcache
      2. Configure Zend OPcache
      3. Use Zend OPcache
    7. Built-in HTTP server
      1. Start the Server
      2. Configure the Server
      3. Router Scripts
      4. Detect the Built-in Server
      5. Drawbacks
    8. What’s Next
  5. II. Good Practices
  6. 3. Standards
    1. PHP-FIG to the Rescue
    2. Framework Interoperability
      1. Interfaces
      2. Autoloading
      3. Style
    3. What Is a PSR?
    4. PSR-1: Basic Code Style
    5. PSR-2: Strict Code Style
    6. PSR-3: Logger Interface
      1. Write a PSR-3 Logger
      2. Use a PSR-3 Logger
    7. PSR-4: Autoloaders
      1. Why Autoloaders Are Important
      2. The PSR-4 Autoloader Strategy
      3. How to Write a PSR-4 Autoloader (and Why You Shouldn’t)
  7. 4. Components
    1. Why Use Components?
    2. What Are Components?
    3. Components Versus Frameworks
      1. Not All Frameworks Are Bad
      2. Use the Right Tool for the Job
    4. Find Components
      1. Shop
      2. Choose
      3. Leave Feedback
    5. Use PHP Components
      1. How to Install Composer
      2. How to Use Composer
      3. Example Project
      4. Composer and Private Repositories
    6. Create PHP Components
      1. Vendor and Package Names
      2. Namespaces
      3. Filesystem Organization
      4. The composer.json File
      5. The <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">README</code> file file
      6. Component Implementation
      7. Version Control
      8. Packagist Submission
      9. Using the Component
  8. 5. Good Practices
    1. Sanitize, Validate, and Escape
      1. Sanitize Input
      2. Validate Data
      3. Escape Output
    2. Passwords
      1. Never Know User Passwords
      2. Never Restrict User Passwords
      3. Never Email User Passwords
      4. Hash User Passwords with bcrypt
      5. Password Hashing API
      6. Password Hashing API for PHP < 5.5.0
    3. Dates, Times, and Time Zones
      1. Set a Default Time Zone
      2. The DateTime Class
      3. The DateInterval Class
      4. The DateTimeZone Class
      5. The DatePeriod Class
      6. The nesbot/carbon Component
    4. Databases
      1. The PDO Extension
      2. Database Connections and DSNs
      3. Prepared Statements
      4. Query Results
      5. Transactions
    5. Multibyte Strings
      1. Character Encoding
      2. Output UTF-8 Data
    6. Streams
      1. Stream Wrappers
      2. Stream Context
      3. Stream Filters
      4. Custom Stream Filters
    7. Errors and Exceptions
      1. Exceptions
      2. Exception Handlers
      3. Errors
      4. Error Handlers
      5. Errors and Exceptions During Development
      6. Production
  9. III. Deployment, Testing, and Tuning
  10. 6. Hosting
    1. Shared Server
    2. Virtual Private Server
    3. Dedicated Server
    4. PaaS
    5. Choose a Hosting Plan
  11. 7. Provisioning
    1. Our Goal
    2. Server Setup
      1. First Login
      2. Software Updates
      3. Nonroot User
      4. SSH Key-Pair Authentication
      5. Disable Passwords and Root Login
    3. PHP-FPM
      1. Install
      2. Global Configuration
      3. Pool Configuration
    4. nginx
      1. Install
      2. Virtual Host
    5. Automate Server Provisioning
    6. Delegate Server Provisioning
    7. Further Reading
    8. What’s Next
  12. 8. Tuning
    1. The php.ini File
    2. Memory
    3. Zend OPcache
    4. File Uploads
    5. Max Execution Time
    6. Session Handling
    7. Output Buffering
    8. Realpath Cache
    9. Up Next
  13. 9. Deployment
    1. Version Control
    2. Automate Deployment
      1. Make It Simple
      2. Make It Predictable
      3. Make It Reversible
    3. Capistrano
      1. How It Works
      2. Install
      3. Configure
      4. Authenticate
      5. Prepare the Remote Server
      6. Capistrano Hooks
      7. Deploy Your Application
      8. Roll Back Your Application
    4. Further Reading
    5. What’s Next
  14. 10. Testing
    1. Why Do We Test?
    2. When Do We Test?
      1. Before
      2. During
      3. After
    3. What Do We Test?
    4. How Do We Test?
      1. Unit Tests
      2. Test-Driven Development (TDD)
      3. Behavior-Driven Development (BDD)
    5. PHPUnit
      1. Directory Structure
      2. Install PHPUnit
      3. Install Xdebug
      4. Configure PHPUnit
      5. The Whovian Class
      6. The WhovianTest Test Case
      7. Run Tests
      8. Code Coverage
    6. Continuous Testing with Travis CI
      1. Setup
      2. Run
    7. Further Reading
    8. What’s Next
  15. 11. Profiling
    1. When to Use a Profiler
    2. Types of Profilers
    3. Xdebug
      1. Configure
      2. Trigger
      3. Analyze
    4. XHProf
      1. Install
      2. XHGUI
      3. Configure
      4. Trigger
    5. New Relic Profiler
    6. Blackfire Profiler
    7. Further Reading
    8. What’s Next
  16. 12. HHVM and Hack
    1. HHVM
      1. PHP at Facebook
      2. HHVM and Zend Engine Parity
      3. Is HHVM Right for Me?
      4. Install
      5. Configure
      6. Extensions
      7. Monitor HHVM with Supervisord
      8. HHVM, FastCGI, and Nginx
    2. The Hack Language
      1. Convert PHP to Hack
      2. What is a Type?
      3. Static Typing
      4. Dynamic Typing
      5. Hack Goes Both Ways
      6. Hack Type Checking
      7. Hack Modes
      8. Hack Syntax
      9. Hack Data Structures
      10. HHVM/Hack vs. PHP
    3. Further Reading
  17. 13. Community
    1. Local PUG
    2. Conferences
    3. Mentoring
    4. Stay Up-to-Date
      1. Websites
      2. Mailing Lists
      3. Twitter
      4. Podcasts
      5. Humor
  18. A. Installing PHP
    1. Linux
      1. Package Managers
      2. Ubuntu 14.04 LTS
      3. CentOS 7
    2. OS X
      1. MAMP
      2. Homebrew
    3. Build from Source
      1. Get the Source Code
    4. Windows
      1. Binaries
      2. WAMP
      3. Zend Server
  19. B. Local Development Environments
    1. VirtualBox
    2. Vagrant
      1. Commands
      2. Boxes
      3. Initialize
      4. Provision
      5. Synced folders
      6. Get started
  20. Index