O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering PHP 7

Book Description

Effective, readable, and robust codes in PHP

About This Book

  • Leverage the newest tools available in PHP 7 to build scalable applications
  • Embrace serverless architecture and the reactive programming paradigm, which are the latest additions to the PHP ecosystem
  • Explore dependency injection and implement design patterns to write elegant code

Who This Book Is For

This book is for intermediate level developers who want to become a master of PHP. Basic knowledge of PHP is required across areas such as basic syntax, types, variables, constants, expressions, operators, control structures, and functions.

What You Will Learn

  • Grasp the current state of PHP language and the PHP standards
  • Effectively implement logging and error handling during development
  • Build services through SOAP and REST and Apache Trift
  • Get to know the benefits of serverless architecture
  • Understand the basic principles of reactive programming to write asynchronous code
  • Practically implement several important design patterns
  • Write efficient code by executing dependency injection
  • See the working of all magic methods
  • Handle the command-line area tools and processes
  • Control the development process with proper debugging and profiling

In Detail

PHP is a server-side scripting language that is widely used for web development. With this book, you will get a deep understanding of the advanced programming concepts in PHP and how to apply it practically

The book starts by unveiling the new features of PHP 7 and walks you through several important standards set by PHP Framework Interop Group (PHP-FIG). You'll see, in detail, the working of all magic methods, and the importance of effective PHP OOP concepts, which will enable you to write effective PHP code. You will find out how to implement design patterns and resolve dependencies to make your code base more elegant and readable. You will also build web services alongside microservices architecture, interact with databases, and work around third-party packages to enrich applications. This book delves into the details of PHP performance optimization. You will learn about serverless architecture and the reactive programming paradigm that found its way in the PHP ecosystem. The book also explores the best ways of testing your code, debugging, tracing, profiling, and deploying your PHP application.

By the end of the book, you will be able to create readable, reliable, and robust applications in PHP to meet modern day requirements in the software industry.

Style and approach

This is a comprehensive, step-by-step practical guide to developing scalable applications using PHP 7.1

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. The All New PHP
    1. Scalar type hints
    2. Return type hints
    3. Anonymous classes
    4. Generator delegation
    5. Generator return expressions
    6. The null coalesce operator
    7. The spaceship operator
    8. Constant arrays
    9. Uniform variable syntax
    10. Throwables
    11. Group use declarations
    12. Catching multiple exceptions types
    13. Class constant visibility modifiers
    14. Iterable pseudo-type
    15. Nullable types
    16. Void return types
    17. Summary
  3. Embracing Standards
    1. PSR-1 - basic coding standard
    2. PSR-2 - coding style guide
    3. PSR-3 - logger interface
    4. PSR-4 - autoloading standard
    5. PSR-6 - caching interface
    6. PSR-7 - HTTP message interface
    7. PSR-13 - hypermedia links
    8. Summary
  4. Error Handling and Logging
    1. Error handling
      1. Error
        1. ArithmeticError
        2. DivisionByZeroError
        3. AssertionError
        4. ParseError
        5. TypeError
        6. Uncaught error handler
        7. Triggering errors
      2. Exception
        1. Creating a custom exception handler
        2. Rethrowing exceptions
        3. Uncaught Exception handler
    2. Logging
      1. Native logging
      2. Logging with Monolog
    3. Summary
  5. Magic Behind Magic Methods
    1. Using __construct()
    2. Using __destruct()
    3. Using __call()
    4. Using __callStatic()
    5. Using __set()
    6. Using __get()
    7. Using __isset()
    8. Using __unset()
    9. Using __sleep()
    10. Using __wakeup()
    11. Using __toString()
    12. Using __invoke()
    13. Using __set_state()
    14. Using __clone()
    15. Using __debugInfo()
    16. Usage statistics across popular platforms
    17. Summary
  6. The Realm of CLI
    1. Understanding PHP CLI
    2. The Console component
      1. Setting up the Console component
      2. Creating a console command
      3. Dealing with inputs
      4. Using Console component helpers
    3. Input/output streams
    4. Process control
      1. Ticks
      2. Signals
      3. Alarms
      4. Multiprocessing
    5. Summary
  7. Prominent OOP Features
    1. Object inheritance
    2. Objects and references
    3. Object iteration
    4. Object comparison
    5. Traits
    6. Reflection
    7. Summary
  8. Optimizing for High Performance
    1. Max execution time
    2. Memory management
    3. File uploads
    4. Session handling
    5. Output buffering
    6. Disabling debug messages
    7. Zend OPcache
    8. Concurrency
    9. Summary
  9. Going Serverless
    1. Using the serverless framework
    2. Using Iron.io IronWorker
    3. Summary
  10. Reactive Programming
    1. Similarities with event-driven programming
    2. Using RxPHP
      1. Installing RxPHP
      2. Observable and observer
      3. Subject
      4. Operator
      5. Writing custom operators
    3. Non-blocking IO
    4. Using React
      1. Installing React
      2. React event loop
      3. Observables and event loop 
    5. Summary
  11. Common Design Patterns
    1. Base patterns
      1. The registry pattern
    2. Creational patterns
      1. The singleton pattern
      2. The prototype pattern
      3. The abstract factory pattern
      4. The builder pattern
      5. The object pool pattern
    3. Behavioral patterns
      1. The strategy pattern
      2. The observer pattern
      3. The lazy initialization pattern
      4. The chain of responsibility pattern
    4. Structural patterns
      1. The decorator pattern
    5. Summary
  12. Building Services
    1. Understanding the client-server relationship
    2. Working with SOAP
      1. XML extensions
      2. Creating server
      3. Creating WSDL file
      4. Creating client
    3. Working with REST
      1. JSON extensions
      2. Creating server
      3. Creating client
    4. Working with Apache Thrift (RPC)
      1. Installing Apache Thrift
      2. Defining service
      3. Creating server
      4. Creating client
    5. Understanding microservices
    6. Summary
  13. Working with Databases
    1. Working with MySQL
      1. Installing MySQL
      2. Setting up sample data
      3. Querying via the MySQLi driver extension
        1. Connecting
        2. Error handling
        3. Selecting 
          1. Binding parameters
        4. Inserting
        5. Updating 
        6. Deleting
        7. Transactions
      4. Querying via the PHP Data Objects driver extension
        1. Connecting
        2. Error handling
        3. Selecting 
        4. Inserting
        5. Updating 
        6. Deleting
        7. Transactions
    2. Working with MongoDB
      1. Installing MongoDB
      2. Setting up sample data
      3. Querying via the MongoDB driver extension
        1. Connecting
        2. Error handling
        3. Selecting 
        4. Inserting
        5. Updating 
        6. Deleting
        7. Transactions
    3. Working with Redis
      1. Installing Redis
      2. Setting up sample data
      3. Querying via the phpredis driver extension
        1. Connecting
        2. Error handling
        3. Selecting 
        4. Inserting
        5. Updating 
        6. Deleting
        7. Transactions
    4. Summary
  14. Resolving Dependencies
    1. Mitigating the common problem
    2. Understanding dependency injection
    3. Understanding dependency injection container
    4. Summary
  15. Working with Packages
    1. Understanding Composer
    2. Understanding Packagist
    3. Using third-party packages
    4. Creating your own package
    5. Distributing your package
    6. Summary
  16. Testing the Important Bits
    1. PHPUnit
      1. Setting up the PHPUnit
      2. Setting up a sample application
      3. Writing test
      4. Executing tests
      5. Code coverage
    2. Behat
      1. Setting up Behat
      2. Setting up a sample application
      3. Writing test
      4. Executing tests
    3. phpspec
      1. Setting up phpspec
      2. Writing test
      3. Executing tests
    4. jMeter
      1. Writing test
      2. Executing tests
    5. Summary
  17. Debugging, Tracing, and Profiling
    1. Xdebug
      1. Installation
      2. Debugging
      3. Tracing
      4. Profiling
    2. Zend Z-Ray
      1. Installing Zend Server
      2. Setting up the virtual host
      3. Using Z-Ray
    3. Summary
  18. Hosting, Provisioning, and Deployment
    1. Choosing the right hosting plan
      1. Shared server
      2. Virtual private server
      3. Dedicated server
      4. PaaS
    2. Automating provisioning
      1. Setting up the workstation machine
      2. Setting up the server machine
      3. Configuring Ansible
      4. Provisioning a web server
    3. Automating the deployment
      1. Installing Deployer
      2. Using Deployer
    4. Continuous integration
      1. Jenkins
    5. Summary