You are previewing PHP 7 Programming Cookbook.
O'Reilly logo
PHP 7 Programming Cookbook

Book Description

Over 80 recipes that will take your PHP 7 web development skills to the next level!

About This Book

  • This is the most up-to-date book in the market on PHP

  • It covers the new features of version 7.x, best practices for server-side programming, and MVC frameworks

  • The recipe-based approach will allow you to explore the unique capabilities that PHP offers to web programmers

  • Who This Book Is For

    If you are an aspiring web developer, mobile developer, or backend programmer, then this book is for you as it will take your PHP programming skills to next level. Basic knowledge of PHP programming is assumed.

    What You Will Learn

  • Use advanced PHP 7 features, such as the Abstract Syntax Tree, Uniform Variable Syntax, Scalar Type Hints, Generator Delegation, Anonymous Classes, and the Context Sensitive Lexer

  • Discover where and when PHP 5 code needs to be re-written to avoid backwards-compatibility breaks

  • Improve the overall application security and error handling by taking advantage of classes that implement the new throwable interface

  • Solve practical real-world programming problems using PHP 7

  • Develop middle-wareclasses that allow PHP developers to gluedifferent open source libraries together seamlessly

  • Define and Implement PSR-7 classes

  • Create custom middleware using PSR-7 compliant classes

  • Test and debug your code, and get to know the best practices

  • In Detail

    PHP 7 comes with a myriad of new features and great tools to optimize your code and make your code perform faster than in previous versions. Most importantly, it allows you to maintain high traffic on your websites with low-cost hardware and servers through a multithreading web server.

    This book demonstrates intermediate to advanced PHP techniques with a focus on PHP 7. Each recipe is designed to solve practical, real-world problems faced by PHP developers like yourself every day. We also cover new ways of writing PHP code made possible only in version 7. In addition, we discuss backward-compatibility breaks and give you plenty of guidance on when and where PHP 5 code needs to be changed to produce the correct results when running under PHP 7. This book also incorporates the latest PHP 7.x features.

    By the end of the book, you will be equipped with the tools and skills required to deliver efficient applications for your websites and enterprises.

    Style and approach

    This book takes a recipe-based approach, with real-world examples that can serve as building blocks for a larger application. Each recipe is self-contained with no external dependencies. This book follows a problem-solution strategy so you understand how to deal with various scenarios you may encounter while using PHP 7 in your daily activities.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. PHP 7 Programming Cookbook
      1. Table of Contents
      2. PHP 7 Programming Cookbook
      3. Credits
      4. Foreword
      5. About the Author
      6. About the Reviewers
      7. www.PacktPub.com
        1. eBooks, discount offers, and more
          1. Why Subscribe?
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Sections
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      9. 1. Building a Foundation
        1. Introduction
        2. PHP 7 installation considerations
          1. How to do it...
            1. Installing directly from source
            2. Installing PHP 7 from pre-compiled binaries
            3. Installing a *AMP package
          2. There's more...
          3. See also
        3. Using the built-in PHP web server
          1. How to do it...
        4. Defining a test MySQL database
          1. How to do it...
        5. Installing PHPUnit
        6. Implementing class autoloading
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Hoovering a website
          1. How to do it...
          2. How it works...
          3. See also
        8. Building a deep web scanner
          1. How to do it...
          2. How it works...
          3. See also
        9. Creating a PHP 5 to PHP 7 code converter
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      10. 2. Using PHP 7 High Performance Features
        1. Introduction
        2. Understanding the abstract syntax tree
          1. How to do it...
          2. How it works...
          3. See also
        3. Understanding differences in parsing
          1. How to do it...
          2. How it works...
          3. See also
        4. Understanding differences in foreach() handling
          1. How to do it...
          2. How it works...
          3. See also
        5. Improving performance using PHP 7 enhancements
          1. How to do it...
          2. How it works...
          3. There's more...
        6. Iterating through a massive file
          1. How to do it...
          2. How it works...
        7. Uploading a spreadsheet into a database
          1. Getting ready...
          2. How to do it...
          3. How it works...
        8. Recursive directory iterator
          1. How to do it...
          2. How it works...
      11. 3. Working with PHP Functional Programming
        1. Introduction
        2. Developing functions
          1. How to do it...
          2. How it works...
        3. Hinting at data types
          1. How to do it...
          2. How it works...
          3. See also
        4. Using return value data typing
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        5. Using iterators
          1. How to do it...
          2. How it works...
        6. Writing your own iterator using generators
          1. How to do it...
          2. How it works...
      12. 4. Working with PHP Object-Oriented Programming
        1. Introduction
        2. Developing classes
          1. How to do it...
          2. How it works...
          3. See also...
        3. Extending classes
          1. How to do it...
          2. How it works...
        4. Using static properties and methods
          1. How to do it...
          2. How it works...
          3. See also
        5. Using namespaces
          1. How to do it...
          2. How it works...
        6. Defining visibility
          1. How to do it...
          2. How it works...
          3. See also
        7. Using interfaces
          1. How to do it...
          2. How it works...
        8. Using traits
          1. How to do it...
          2. How it works...
        9. Implementing anonymous classes
          1. How to do it...
          2. How it works...
      13. 5. Interacting with a Database
        1. Introduction
        2. Using PDO to connect to a database
          1. How to do it...
          2. How it works...
          3. See also
        3. Building an OOP SQL query builder
          1. How to do it...
          2. How it works...
          3. See also
        4. Handling pagination
          1. How to do it...
          2. How it works...
          3. See also
        5. Defining entities to match database tables
          1. How to do it...
          2. How it works...
          3. See also
        6. Tying entity classes to RDBMS queries
          1. How to do it...
          2. How it works...
          3. There's more...
        7. Embedding secondary lookups into query results
          1. How to do it...
          2. How it works...
        8. Implementing jQuery DataTables PHP lookups
          1. How to do it...
          2. How it works...
          3. There's more...
      14. 6. Building Scalable Websites
        1. Introduction
        2. Creating a generic form element generator
          1. How to do it...
          2. How it works...
        3. Creating an HTML radio element generator
          1. How to do it...
          2. How it works...
          3. There's more...
        4. Creating an HTML select element generator
          1. How to do it...
          2. How it works...
        5. Implementing a form factory
          1. How to do it...
          2. How it works...
        6. Chaining $_POST filters
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        7. Chaining $_POST validators
          1. How to do it...
          2. How it works...
        8. Tying validation to a form
          1. How to do it...
          2. How it works...
      15. 7. Accessing Web Services
        1. Introduction
        2. Converting between PHP and XML
          1. How to do it...
          2. How it works...
        3. Creating a simple REST client
          1. How to do it...
            1. Creating a streams-based REST client
            2. Defining a cURL-based REST client
          2. How it works...
          3. There's more...
          4. See also
        4. Creating a simple REST server
          1. How to do it...
          2. How it works...
          3. There's more...
        5. Creating a simple SOAP client
          1. How to do it...
          2. How it works...
          3. See also
        6. Creating a simple SOAP server
          1. How to do it...
          2. How it works...
          3. See also
      16. 8. Working with Date/Time and International Aspects
        1. Introduction
        2. Using emoticons or emoji in a view script
          1. How to do it...
          2. How it works...
          3. See also
        3. Converting complex characters
          1. How to do it...
          2. How it works...
        4. Getting the locale from browser data
          1. How to do it...
          2. How it works...
          3. See also
        5. Formatting numbers by locale
          1. How to do it...
          2. How it works...
          3. See also
        6. Handling currency by locale
          1. How to do it...
          2. How it works...
          3. See also
        7. Formatting date/time by locale
          1. How to do it...
          2. How it works...
          3. See also
        8. Creating an HTML international calendar generator
          1. How to do it...
            1. Refining internationalized output
          2. How it works...
          3. See also
        9. Building a recurring events generator
          1. How to do it...
          2. How it works...
          3. See also
        10. Handling translation without gettext
          1. How to do it...
          2. How it works...
          3. See also
      17. 9. Developing Middleware
        1. Introduction
        2. Authenticating with middleware
          1. How to do it...
          2. How it works...
          3. See also
        3. Using middleware to implement access control
          1. How to do it...
          2. How it works...
          3. See also
        4. Improving performance using the cache
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        5. Implementing routing
          1. How to do it...
          2. How it works...
          3. See also
        6. Making inter-framework system calls
          1. How to do it...
          2. How it works...
        7. Using middleware to cross languages
          1. How to do it...
      18. 10. Looking at Advanced Algorithms
        1. Introduction
        2. Using getters and setters
          1. How to do it...
          2. How it works...
        3. Implementing a linked list
          1. How to do it...
          2. How it works...
          3. There's more...
        4. Building a bubble sort
          1. How to do it...
          2. How it works...
        5. Implementing a stack
          1. How to do it...
          2. How it works...
        6. Building a binary search class
          1. How to do it...
          2. How it works...
          3. See also
        7. Implementing a search engine
          1. How to do it...
          2. How it works...
        8. Displaying a multi-dimensional array and accumulating totals
          1. How to do it...
          2. How it works...
      19. 11. Implementing Software Design Patterns
        1. Introduction
        2. Creating an array to object hydrator
          1. How to do it...
          2. How it works...
        3. Building an object to array hydrator
          1. How to do it...
          2. How it works...
        4. Implementing a strategy pattern
          1. How to do it...
          2. How it works...
        5. Defining a mapper
          1. How to do it...
          2. How it works...
        6. Implementing object-relational mapping
          1. How to do it...
            1. Technique #1 - pre-loading all child information
            2. Technique #2 - embedding secondary lookups
          2. How it works...
          3. See also
        7. Implementing the Pub/Sub design pattern
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
      20. 12. Improving Web Security
        1. Introduction
        2. Filtering $_POST data
          1. How to do it...
          2. How it works...
          3. See also
        3. Validating $_POST data
          1. How to do it...
          2. How it works...
          3. See also
        4. Safeguarding the PHP session
          1. How to do it...
          2. How it works...
          3. See also
        5. Securing forms with a token
          1. How to do it...
          2. How it works...
          3. See also
        6. Building a secure password generator
          1. How to do it...
          2. How it works...
          3. See also
        7. Safeguarding forms with a CAPTCHA
          1. How to do it...
            1. Generating a text CAPTCHA
            2. Generating an image CAPTCHA
          2. How it works...
          3. There's more...
          4. See also
        8. Encrypting/decrypting without mcrypt
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
      21. 13. Best Practices, Testing, and Debugging
        1. Introduction
        2. Using Traits and Interfaces
          1. How to do it...
          2. How it works...
        3. Universal exception handler
          1. How to do it...
          2. How it works...
          3. See also
        4. Universal error handler
          1. How to do it...
          2. How it works...
          3. See also
        5. Writing a simple test
          1. How to do it...
            1. Running simple tests
            2. Testing database Model classes
            3. Using mock classes
            4. Using anonymous classes as mock objects
            5. Using Mock Builder
          2. How it works...
            1. Running simple tests
            2. Testing database model classes
            3. Using mock classes
          3. There's more...
          4. See also...
        6. Writing a test suite
          1. How to do it...
          2. How it works...
          3. See also...
        7. Generating fake test data
          1. How to do it...
          2. How it works...
          3. There's more...
        8. Customizing sessions using session_start parameters
          1. How to do it...
          2. How it works...
          3. See also...
      22. A. Defining PSR-7 Classes
        1. Introduction
        2. Implementing PSR-7 value object classes
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Developing a PSR-7 Request class
          1. How to do it...
          2. How it works...
          3. See also
        4. Defining a PSR-7 Response class
          1. How to do it...
          2. How it works...
          3. See also
      23. Index