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

PHP 7: Real World Application Development

Book Description

Use new features of PHP 7 to solve practical, real-world problems faced by PHP developers like yourself every day.

About This Book
  • This course covers the new features of version 7.x, best practices for server-side programming, and MVC frameworks
  • Leverage the potential of PHP for server-side programming, memory management, and Object-Oriented Programming to improve your programming productivity
  • This course also illustrates the development of a complete modular application using PHP 7 in detail
Who This Book Is For

If you are an aspiring web developer, mobile developer, or back-end programmer, who has basic experience in PHP programming and wants to develop performance-critical applications, then this course is for you. It will take your PHP programming skills to next level.

What You Will Learn
  • Solve practical real-world programming problems using PHP 7
  • Discover where and when PHP 5 code needs to be re-written to avoid backwards-compatibility breaks
  • 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
  • Set up a high performance development and production environment for PHP 7
  • Discover new OOP features in PHP 7 to achieve high performance
  • Discover the new features of PHP 7 that are relevant to modular application development
  • Explore the ins and outs of the Symfony framework
  • Build a set of modules based on the Symfony framework that comprise a simple web shop app
In Detail

PHP is a great language for developing web applications. It is essentially a server-side scripting language. PHP 7 is the latest version, providing major backward-compatibility breaks and focusing on improved performance and speed.

This course follows a learning path which is divided into three modules. Each module is a mini course in its own right, taking your basic PHP programing skills to the next level by showing you intermediate to advanced PHP techniques with a focus on PHP 7. This way, get you equipped with the tools and skills required to develop professional and efficient applications for your websites and enterprises.

The first module of the book is a programming cookbook that consists over 80 recipes! Each recipe is designed to solve practical, real-world problems faced by PHP developers like yourself every day. This course also covers new ways of writing PHP code made possible only in version 7.

The second module of the course is designed to improve the performance and productivity of your application. We’ll introduce you to the concepts of Object-Oriented Programming (OOP) in PHP 7, then shed some light on how to improve the performance of your PHP 7 applications and database. Throughout this module you will be introduced to benchmarking tools.

With all important concepts of PHP covered up you will move on to third module. In this module you will gain a deep insight into the modular programming paradigm and how to achieve modularity in your PHP code. Modular design techniques help you build readable, manageable, reusable, and more efficient codes. PHP 7, which is a popular open source scripting language, is used to build modular functions for your software.

This Learning Path combines some of the best that Packt has to offer in one complete, curated package. It includes content from the following Packt products:

  • PHP 7 Programming Cookbook, Doug Bierer
  • Learning PHP 7 High Performance, Altaf Hussain
  • Modular Programming with PHP 7, Branko Ajzele
Style and approach

This book takes a practical, step-by-step approach with real-world examples that serve as building blocks for your application development and guide you through improving the quality of your code.

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: Real World Application Development
    1. Table of Contents
    2. PHP 7: Real World Application Development
    3. PHP 7: Real World Application Development
    4. Credits
    5. Preface
      1. What this learning path covers
      2. What you need for this learning path
      3. Who this learning path is for
      4. Reader feedback
      5. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    6. 1. Module 1
      1. 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
      2. 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...
      3. 3. Working with PHP Functions
        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...
      4. 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...
      5. 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...
      6. 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...
      7. 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
      8. 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
      9. 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...
      10. 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...
      11. 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
      12. 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
      13. 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...
      14. 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
    7. 2. Module 2
      1. 1. Setting Up the Environment
        1. Setting up Windows
        2. Setting up Debian or Ubuntu
          1. Debian
          2. Ubuntu
        3. Setting up CentOS
          1. Installing NGINX
          2. Installing PHP 7
          3. Installing Percona Server
        4. Setting up Vagrant
        5. Summary
      2. 2. New Features in PHP 7
        1. OOP features
          1. Type hints
            1. Scalar type hints
            2. Return type hints
          2. Namespaces and group use declaration
            1. Non mixed group use declarations
            2. Mixed group use declarations
            3. The compound namespace declaration
          3. The anonymous classes
          4. Old-style constructor deprecation
          5. The throwable interface
            1. Error
        2. New operators
          1. The Spaceship operator (<=>)
          2. The null coalesce operator(??)
        3. Uniform variable syntax
        4. Miscellaneous features and changes
          1. Constant arrays
          2. Multiple default cases in the switch statement
          3. The options array for session_start function
          4. Filtered unserialize function
        5. Summary
      3. 3. Improving PHP 7 Application Performance
        1. NGINX and Apache
          1. Apache
          2. NGINX
        2. HTTP server optimization
          1. Caching static files
            1. Apache
            2. NGINX
        3. HTTP persistent connection
          1. Apache
          2. NGINX
          3. GZIP compression
            1. Apache
            2. NGINX
          4. Using PHP as a separate service
          5. Disabling unused modules
            1. Apache
            2. NGINX
          6. Web server resources
            1. NGINX
        4. Content Delivery Network (CDN)
          1. Using CDN
        5. CSS and JavaScript optimization
          1. Merging
          2. Minifying
            1. Minify
            2. Grunt
        6. Full page caching
        7. Varnish
        8. The infrastructure
          1. Web servers
          2. The database server
          3. Load balancer (LB)
          4. HAProxy load balancing
            1. HAProxy installation
            2. HAProxy load balancing
        9. Summary
      4. 4. Improving Database Performance
        1. The MySQL database
          1. Query caching
        2. Storage engines
          1. The MyISAM storage engine
          2. The InnoDB storage engine
            1. innodb_buffer_pool_size
            2. innodb_buffer_pool_instances
            3. innodb_log_file_size
        3. The Percona Server - a fork of MySQL
          1. Installing the Percona Server
        4. MySQL performance monitoring tools
          1. phpMyAdmin
          2. The MySQL workbench
          3. Percona Toolkit
            1. pt-query-digest
            2. pt-duplicate-key-checker
        5. Percona XtraDB Cluster (PXC)
        6. Redis – the key-value cache store
          1. Connecting with the Redis server
          2. Storing and fetching data from the Redis server
          3. Redis management tools
        7. Memcached key-value cache store
        8. Summary
      5. 5. Debugging and Profiling
        1. Xdebug
          1. Debugging with Sublime Text
          2. Debugging with Eclipse
        2. Profiling with Xdebug
        3. PHP DebugBar
        4. Summary
      6. 6. Stress/Load Testing PHP Applications
        1. Apache JMeter
        2. ApacheBench (ab)
        3. Siege
        4. Load testing real-world applications
          1. Magento 2
          2. WordPress 4
          3. Drupal 8
        5. Summary
      7. 7. Best Practices in PHP Programming
        1. Coding styles
        2. Test-driven development (TDD)
        3. Design patterns
        4. Service-oriented architecture (SOA)
        5. Being object-oriented and reusable always
        6. PHP frameworks
        7. Version control system (VCS) and Git
        8. Deployment and Continuous Integration (CI)
        9. Summary
      8. A. Tools to Make Life Easy
        1. Composer – A dependency manager for PHP
          1. Composer installation
          2. Using Composer
        2. Git – A version control system
          1. Git installation
          2. Using Git
          3. Creating new branches and merging
          4. Cloning a repository
          5. Webhooks
          6. Desktop tools to manage repositories
        3. Grunt watch
        4. Summary
      9. B. MVC and Frameworks
        1. The MVC design pattern
          1. Model
          2. Views
          3. Controllers
        2. Laravel
          1. Installation
          2. Features
            1. Routing
          3. Eloquent ORM
          4. Artisan CLI
          5. Migrations
          6. Blade templates
          7. Other features
        3. Lumen
        4. Apigility
        5. Summary
    8. 3. Module 3
      1. 1. Ecosystem Overview
        1. Getting ready for PHP 7
          1. Scalar type hints
          2. Return type hints
          3. Anonymous classes
          4. The Closure::call() method
          5. Generator delegation
          6. Generator return expressions
          7. The null coalesce operator
          8. The Spaceship operator
          9. Throwables
          10. The \ParseError
          11. Level support for the dirname() function
          12. The integer division function
          13. Constant arrays
          14. Uniform variable syntax
          15. Secure random number generator
          16. Filtered unserialize()
          17. Context sensitive lexer
          18. Group use declarations
          19. Unicode enhancements
          20. Assertions
          21. Changes to the list() construct
          22. Session options
          23. Deprecated features
          24. Frameworks
            1. Laravel framework
            2. Symfony
            3. Zend Framework
            4. CodeIgniter
            5. CakePHP
            6. Slim
            7. Yii
            8. Phalcon
        2. Summary
      2. 2. GoF Design Patterns
        1. Creational patterns
          1. Abstract factory pattern
          2. Builder pattern
          3. Factory method pattern
          4. Prototype pattern
          5. Singleton pattern
        2. Structural patterns
          1. Adapter pattern
          2. Bridge pattern
          3. Composite pattern
          4. Decorator pattern
          5. Facade pattern
          6. Flyweight pattern
          7. Proxy pattern
        3. Behavioral patterns
          1. Chain of responsibility pattern
          2. Command pattern
          3. Interpreter pattern
          4. Iterator pattern
          5. Mediator pattern
          6. Memento pattern
          7. Observer pattern
          8. State pattern
          9. Strategy pattern
          10. Template pattern
          11. Visitor pattern
        4. Summary
      3. 3. SOLID Design Principles
        1. Single responsibility principle
        2. Open/closed principle
        3. Liskov substitution principle
        4. Interface Segregation Principle
        5. Dependency inversion principle
        6. Summary
      4. 4. Requirement Specification for a Modular Web Shop App
        1. Defining application requirements
        2. Wireframing
        3. Defining a technology stack
          1. The Symfony framework
          2. Foundation framework
        4. Summary
      5. 5. Symfony at a Glance
        1. Installing Symfony
        2. Creating a blank project
        3. Using Symfony console
        4. Controller
        5. Routing
        6. Templates
        7. Forms
        8. Configuring Symfony
        9. The bundle system
        10. Databases and Doctrine
        11. Testing
        12. Validation
        13. Summary
      6. 6. Building the Core Module
        1. Requirements
        2. Dependencies
        3. Implementation
          1. Configuring application-wide security
        4. Unit testing
        5. Functional testing
        6. Summary
      7. 7. Building the Catalog Module
        1. Requirements
        2. Dependencies
        3. Implementation
          1. Creating entities
          2. Managing image uploads
          3. Overriding core module services
          4. Setting up a Category page
          5. Setting up a Product page
        4. Unit testing
        5. Functional testing
        6. Summary
      8. 8. Building the Customer Module
        1. Requirements
        2. Dependencies
        3. Implementation
          1. Creating a customer entity
          2. Modifying the security configuration
          3. Extending the customer entity
          4. Creating the orders service
          5. Creating the customer menu service
          6. Implementing the register process
          7. Implementing the login process
          8. Implementing the logout process
          9. Managing forgotten passwords
        4. Unit testing
        5. Functional testing
        6. Summary
      9. 9. Building the Payment Module
        1. Requirements
        2. Dependencies
        3. Implementation
          1. Creating a card entity
            1. Creating a card payment service
          2. Creating a card payment controller and routes
          3. Creating a check money payment service
          4. Creating a check money payment controller and routes
        4. Unit testing
        5. Functional testing
        6. Summary
      10. 10. Building the Shipment Module
        1. Requirements
        2. Dependencies
        3. Implementation
          1. Creating a flat rate shipment service
          2. Creating a flat rate shipment controller and routes
          3. Creating a dynamic rate payment service
          4. Creating a dynamic rate shipment controller and routes
        4. Unit testing
        5. Functional testing
        6. Summary
      11. 11. Building the Sales Module
        1. Requirements
        2. Dependencies
        3. Implementation
          1. Creating a Cart entity
          2. Creating the cart item entity
          3. Creating an Order entity
          4. Creating a SalesOrderItem entity
          5. Overriding the add_to_cart_url service
          6. Overriding the checkout_menu service
          7. Overriding the customer orders service
          8. Overriding the bestsellers service
          9. Creating the Cart page
          10. Creating the Payment service
          11. Creating the Shipment service
          12. Creating the Checkout page
          13. Creating the order success page
          14. Creating a store manager dashboard
        4. Unit testing
        5. Functional testing
        6. Summary
      12. 12. Integrating and Distributing Modules
        1. Understanding Git
        2. Understanding GitHub
        3. Understanding Composer
        4. Understanding Packagist
        5. Summary
    9. Bibliography
    10. Index