PHP 7: Real World Application Development
By Doug Bierer, Ajzele Branko and Altaf Hussain
()
About this ebook
Read more from Doug Bierer
PHP 7 Programming Cookbook Rating: 0 out of 5 stars0 ratingsMongoDB 4 Quick Start Guide: Learn the skills you need to work with the world's most popular NoSQL database Rating: 0 out of 5 stars0 ratings
Related to PHP 7
Related ebooks
PHP jQuery Cookbook Rating: 0 out of 5 stars0 ratingsJavaScript JSON Cookbook Rating: 0 out of 5 stars0 ratingsLaravel 5.x Cookbook Rating: 0 out of 5 stars0 ratingsLaravel Application Development Cookbook Rating: 0 out of 5 stars0 ratingsXamarin Mobile Development for Android Cookbook Rating: 0 out of 5 stars0 ratingsNode Cookbook: Second Edition Rating: 3 out of 5 stars3/5HTML5 Data and Services Cookbook Rating: 5 out of 5 stars5/5PHP 7 Programming Blueprints Rating: 0 out of 5 stars0 ratingsThe PHP Workshop: Learn to build interactive applications and kickstart your career as a web developer Rating: 0 out of 5 stars0 ratingsMastering PHP 7 Rating: 1 out of 5 stars1/5Mastering PHP Design Patterns Rating: 0 out of 5 stars0 ratingsPHP Reactive Programming Rating: 0 out of 5 stars0 ratingsLearning PHP 7 Rating: 4 out of 5 stars4/5Learning PHP 7 High Performance Rating: 0 out of 5 stars0 ratingsMastering JavaScript Rating: 4 out of 5 stars4/5Instant SASS CSS How-to Rating: 5 out of 5 stars5/5HTML, CSS, Bootstrap, Php, Javascript and MySql: All you need to know to create a dynamic site Rating: 4 out of 5 stars4/5Object-Oriented JavaScript Rating: 4 out of 5 stars4/5PHP Examples Part 4 Rating: 0 out of 5 stars0 ratingsJavaScript Unlocked Rating: 5 out of 5 stars5/5Professional CSS3 Rating: 5 out of 5 stars5/5phpMyAdmin Starter Rating: 0 out of 5 stars0 ratingsLaravel 5 Essentials Rating: 0 out of 5 stars0 ratingsResponsive Web Design with HTML5 and CSS3 - Second Edition Rating: 4 out of 5 stars4/5Modular Programming with PHP 7 Rating: 0 out of 5 stars0 ratingsMongoose for Application Development Rating: 5 out of 5 stars5/5Learning jQuery 3 - Fifth Edition Rating: 0 out of 5 stars0 ratingsObject-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications, and libraries Rating: 3 out of 5 stars3/5Modern JavaScript Applications Rating: 0 out of 5 stars0 ratingsLearning Vue.js 2 Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsHTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5C Programming For Beginners: The Simple Guide to Learning C Programming Language Fast! Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsBeginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Programming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5Coding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratingsC# Programming from Zero to Proficiency (Beginner): C# from Zero to Proficiency, #2 Rating: 0 out of 5 stars0 ratings
Reviews for PHP 7
0 ratings0 reviews
Book preview
PHP 7 - Doug Bierer
Table of Contents
PHP 7: Real World Application Development
PHP 7: Real World Application Development
Credits
Preface
What this learning path covers
What you need for this learning path
Who this learning path is for
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Module 1
1. Building a Foundation
Introduction
PHP 7 installation considerations
How to do it...
Installing directly from source
Installing PHP 7 from pre-compiled binaries
Installing a *AMP package
There's more...
See also
Using the built-in PHP web server
How to do it...
Defining a test MySQL database
How to do it...
Installing PHPUnit
Implementing class autoloading
Getting ready
How to do it...
How it works...
Hoovering a website
How to do it...
How it works...
See also
Building a deep web scanner
How to do it...
How it works...
See also
Creating a PHP 5 to PHP 7 code converter
Getting ready
How to do it...
How it works...
See also
2. Using PHP 7 High Performance Features
Introduction
Understanding the abstract syntax tree
How to do it...
How it works...
See also
Understanding differences in parsing
How to do it...
How it works...
See also
Understanding differences in foreach() handling
How to do it...
How it works...
See also
Improving performance using PHP 7 enhancements
How to do it...
How it works...
There's more...
Iterating through a massive file
How to do it...
How it works...
Uploading a spreadsheet into a database
Getting ready...
How to do it...
How it works...
Recursive directory iterator
How to do it...
How it works...
3. Working with PHP Functions
Introduction
Developing functions
How to do it...
How it works...
Hinting at data types
How to do it...
How it works...
See also
Using return value data typing
How to do it...
How it works...
There's more...
See also
Using iterators
How to do it...
How it works...
Writing your own iterator using generators
How to do it...
How it works...
4. Working with PHP Object-Oriented Programming
Introduction
Developing classes
How to do it...
How it works...
See also...
Extending classes
How to do it...
How it works...
Using static properties and methods
How to do it...
How it works...
See also
Using namespaces
How to do it...
How it works...
Defining visibility
How to do it...
How it works...
See also
Using interfaces
How to do it...
How it works...
Using traits
How to do it...
How it works...
Implementing anonymous classes
How to do it...
How it works...
5. Interacting with a Database
Introduction
Using PDO to connect to a database
How to do it...
How it works...
See also
Building an OOP SQL query builder
How to do it...
How it works...
See also
Handling pagination
How to do it...
How it works...
See also
Defining entities to match database tables
How to do it...
How it works...
See also
Tying entity classes to RDBMS queries
How to do it...
How it works...
There's more...
Embedding secondary lookups into query results
How to do it...
How it works...
Implementing jQuery DataTables PHP lookups
How to do it...
How it works...
There's more...
6. Building Scalable Websites
Introduction
Creating a generic form element generator
How to do it...
How it works...
Creating an HTML radio element generator
How to do it...
How it works...
There's more...
Creating an HTML select element generator
How to do it...
How it works...
Implementing a form factory
How to do it...
How it works...
Chaining $_POST filters
How to do it...
How it works...
There's more...
See also
Chaining $_POST validators
How to do it...
How it works...
Tying validation to a form
How to do it...
How it works...
7. Accessing Web Services
Introduction
Converting between PHP and XML
How to do it...
How it works...
Creating a simple REST client
How to do it...
Creating a streams-based REST client
Defining a cURL-based REST client
How it works...
There's more...
See also
Creating a simple REST server
How to do it...
How it works...
There's more...
Creating a simple SOAP client
How to do it...
How it works...
See also
Creating a simple SOAP server
How to do it...
How it works...
See also
8. Working with Date/Time and International Aspects
Introduction
Using emoticons or emoji in a view script
How to do it...
How it works...
See also
Converting complex characters
How to do it...
How it works...
Getting the locale from browser data
How to do it...
How it works...
See also
Formatting numbers by locale
How to do it...
How it works...
See also
Handling currency by locale
How to do it...
How it works...
See also
Formatting date/time by locale
How to do it...
How it works...
See also
Creating an HTML international calendar generator
How to do it...
Refining internationalized output
How it works...
See also
Building a recurring events generator
How to do it...
How it works...
See also
Handling translation without gettext
How to do it...
How it works...
See also
9. Developing Middleware
Introduction
Authenticating with middleware
How to do it...
How it works...
See also
Using middleware to implement access control
How to do it...
How it works...
See also
Improving performance using the cache
How to do it...
How it works...
There's more...
See also
Implementing routing
How to do it...
How it works...
See also
Making inter-framework system calls
How to do it...
How it works...
Using middleware to cross languages
How to do it...
10. Looking at Advanced Algorithms
Introduction
Using getters and setters
How to do it...
How it works...
Implementing a linked list
How to do it...
How it works...
There's more...
Building a bubble sort
How to do it...
How it works...
Implementing a stack
How to do it...
How it works...
Building a binary search class
How to do it...
How it works...
See also
Implementing a search engine
How to do it...
How it works...
Displaying a multi-dimensional array and accumulating totals
How to do it...
How it works...
11. Implementing Software Design Patterns
Introduction
Creating an array to object hydrator
How to do it...
How it works...
Building an object to array hydrator
How to do it...
How it works...
Implementing a strategy pattern
How to do it...
How it works...
Defining a mapper
How to do it...
How it works...
Implementing object-relational mapping
How to do it...
Technique #1 - pre-loading all child information
Technique #2 - embedding secondary lookups
How it works...
See also
Implementing the Pub/Sub design pattern
How to do it...
How it works...
There's more...
See also
12. Improving Web Security
Introduction
Filtering $_POST data
How to do it...
How it works...
See also
Validating $_POST data
How to do it...
How it works...
See also
Safeguarding the PHP session
How to do it...
How it works...
See also
Securing forms with a token
How to do it...
How it works...
See also
Building a secure password generator
How to do it...
How it works...
See also
Safeguarding forms with a CAPTCHA
How to do it...
Generating a text CAPTCHA
Generating an image CAPTCHA
How it works...
There's more...
See also
Encrypting/decrypting without mcrypt
How to do it...
How it works...
There's more...
See also
13. Best Practices, Testing, and Debugging
Introduction
Using Traits and Interfaces
How to do it...
How it works...
Universal exception handler
How to do it...
How it works...
See also
Universal error handler
How to do it...
How it works...
See also
Writing a simple test
How to do it...
Running simple tests
Testing database Model classes
Using mock classes
Using anonymous classes as mock objects
Using Mock Builder
How it works...
Running simple tests
Testing database model classes
Using mock classes
There's more...
See also...
Writing a test suite
How to do it...
How it works...
See also...
Generating fake test data
How to do it...
How it works...
There's more...
Customizing sessions using session_start parameters
How to do it...
How it works...
See also...
A. Defining PSR-7 Classes
Introduction
Implementing PSR-7 value object classes
Getting ready
How to do it...
How it works...
See also
Developing a PSR-7 Request class
How to do it...
How it works...
See also
Defining a PSR-7 Response class
How to do it...
How it works...
See also
2. Module 2
1. Setting Up the Environment
Setting up Windows
Setting up Debian or Ubuntu
Debian
Ubuntu
Setting up CentOS
Installing NGINX
Installing PHP 7
Installing Percona Server
Setting up Vagrant
Summary
2. New Features in PHP 7
OOP features
Type hints
Scalar type hints
Return type hints
Namespaces and group use declaration
Non mixed group use declarations
Mixed group use declarations
The compound namespace declaration
The anonymous classes
Old-style constructor deprecation
The throwable interface
Error
New operators
The Spaceship operator (<=>)
The null coalesce operator(??)
Uniform variable syntax
Miscellaneous features and changes
Constant arrays
Multiple default cases in the switch statement
The options array for session_start function
Filtered unserialize function
Summary
3. Improving PHP 7 Application Performance
NGINX and Apache
Apache
NGINX
HTTP server optimization
Caching static files
Apache
NGINX
HTTP persistent connection
Apache
NGINX
GZIP compression
Apache
NGINX
Using PHP as a separate service
Disabling unused modules
Apache
NGINX
Web server resources
NGINX
Content Delivery Network (CDN)
Using CDN
CSS and JavaScript optimization
Merging
Minifying
Minify
Grunt
Full page caching
Varnish
The infrastructure
Web servers
The database server
Load balancer (LB)
HAProxy load balancing
HAProxy installation
HAProxy load balancing
Summary
4. Improving Database Performance
The MySQL database
Query caching
Storage engines
The MyISAM storage engine
The InnoDB storage engine
innodb_buffer_pool_size
innodb_buffer_pool_instances
innodb_log_file_size
The Percona Server - a fork of MySQL
Installing the Percona Server
MySQL performance monitoring tools
phpMyAdmin
The MySQL workbench
Percona Toolkit
pt-query-digest
pt-duplicate-key-checker
Percona XtraDB Cluster (PXC)
Redis – the key-value cache store
Connecting with the Redis server
Storing and fetching data from the Redis server
Redis management tools
Memcached key-value cache store
Summary
5. Debugging and Profiling
Xdebug
Debugging with Sublime Text
Debugging with Eclipse
Profiling with Xdebug
PHP DebugBar
Summary
6. Stress/Load Testing PHP Applications
Apache JMeter
ApacheBench (ab)
Siege
Load testing real-world applications
Magento 2
WordPress 4
Drupal 8
Summary
7. Best Practices in PHP Programming
Coding styles
Test-driven development (TDD)
Design patterns
Service-oriented architecture (SOA)
Being object-oriented and reusable always
PHP frameworks
Version control system (VCS) and Git
Deployment and Continuous Integration (CI)
Summary
A. Tools to Make Life Easy
Composer – A dependency manager for PHP
Composer installation
Using Composer
Git – A version control system
Git installation
Using Git
Creating new branches and merging
Cloning a repository
Webhooks
Desktop tools to manage repositories
Grunt watch
Summary
B. MVC and Frameworks
The MVC design pattern
Model
Views
Controllers
Laravel
Installation
Features
Routing
Eloquent ORM
Artisan CLI
Migrations
Blade templates
Other features
Lumen
Apigility
Summary
3. Module 3
1. Ecosystem Overview
Getting ready for PHP 7
Scalar type hints
Return type hints
Anonymous classes
The Closure::call() method
Generator delegation
Generator return expressions
The null coalesce operator
The Spaceship operator
Throwables
The \ParseError
Level support for the dirname() function
The integer division function
Constant arrays
Uniform variable syntax
Secure random number generator
Filtered unserialize()
Context sensitive lexer
Group use declarations
Unicode enhancements
Assertions
Changes to the list() construct
Session options
Deprecated features
Frameworks
Laravel framework
Symfony
Zend Framework
CodeIgniter
CakePHP
Slim
Yii
Phalcon
Summary
2. GoF Design Patterns
Creational patterns
Abstract factory pattern
Builder pattern
Factory method pattern
Prototype pattern
Singleton pattern
Structural patterns
Adapter pattern
Bridge pattern
Composite pattern
Decorator pattern
Facade pattern
Flyweight pattern
Proxy pattern
Behavioral patterns
Chain of responsibility pattern
Command pattern
Interpreter pattern
Iterator pattern
Mediator pattern
Memento pattern
Observer pattern
State pattern
Strategy pattern
Template pattern
Visitor pattern
Summary
3. SOLID Design Principles
Single responsibility principle
Open/closed principle
Liskov substitution principle
Interface Segregation Principle
Dependency inversion principle
Summary
4. Requirement Specification for a Modular Web Shop App
Defining application requirements
Wireframing
Defining a technology stack
The Symfony framework
Foundation framework
Summary
5. Symfony at a Glance
Installing Symfony
Creating a blank project
Using Symfony console
Controller
Routing
Templates
Forms
Configuring Symfony
The bundle system
Databases and Doctrine
Testing
Validation
Summary
6. Building the Core Module
Requirements
Dependencies
Implementation
Configuring application-wide security
Unit testing
Functional testing
Summary
7. Building the Catalog Module
Requirements
Dependencies
Implementation
Creating entities
Managing image uploads
Overriding core module services
Setting up a Category page
Setting up a Product page
Unit testing
Functional testing
Summary
8. Building the Customer Module
Requirements
Dependencies
Implementation
Creating a customer entity
Modifying the security configuration
Extending the customer entity
Creating the orders service
Creating the customer menu service
Implementing the register process
Implementing the login process
Implementing the logout process
Managing forgotten passwords
Unit testing
Functional testing
Summary
9. Building the Payment Module
Requirements
Dependencies
Implementation
Creating a card entity
Creating a card payment service
Creating a card payment controller and routes
Creating a check money payment service
Creating a check money payment controller and routes
Unit testing
Functional testing
Summary
10. Building the Shipment Module
Requirements
Dependencies
Implementation
Creating a flat rate shipment service
Creating a flat rate shipment controller and routes
Creating a dynamic rate payment service
Creating a dynamic rate shipment controller and routes
Unit testing
Functional testing
Summary
11. Building the Sales Module
Requirements
Dependencies
Implementation
Creating a Cart entity
Creating the cart item entity
Creating an Order entity
Creating a SalesOrderItem entity
Overriding the add_to_cart_url service
Overriding the checkout_menu service
Overriding the customer orders service
Overriding the bestsellers service
Creating the Cart page
Creating the Payment service
Creating the Shipment service
Creating the Checkout page
Creating the order success page
Creating a store manager dashboard
Unit testing
Functional testing
Summary
12. Integrating and Distributing Modules
Understanding Git
Understanding GitHub
Understanding Composer
Understanding Packagist
Summary
Bibliography
Index
PHP 7: Real World Application Development
PHP 7: Real World Application Development
Use new features of PHP 7 to solve practical, real-world problems faced by PHP developers like yourself every day.
A course in three modules
BIRMINGHAM - MUMBAI
PHP 7: Real World Application Development
Copyright © 2016 Packt Publishing
All rights reserved. No part of this course may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this course to ensure the accuracy of the information presented. However, the information contained in this course is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this course.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this course by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Published on: Month 2011
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78712-900-9
www.packtpub.com
Credits
Authors
Doug Bierer
Altaf Hussain
Paul Jones
Reviewers
Salvatore Pappalardo
Vincenzo Provenza
Raul Mesa Ros
Adam Culp
Content Development Editor
Onkar Wani
Graphics
Abhinash Sahu
Production Coordinator
Shraddha Falebhai
Preface
PHP 7 has taken the open source community by storm, breaking records for speed, which is, metaphorically, causing heads to turn. In its most fundamental sense, the core engineering team has effected a major rewrite of the language but has still managed to maintain backward compatibility to a high degree. PHP is a great language for developing web applications. It is essentially a server-side scripting language that is also used for general-purpose programming. PHP 7 is the latest version, providing major backward-compatibility breaks and focusing on improved performance and speed. This means you can maintain high traffic on your websites with low-cost hardware and servers through a multithreading web server.
What this learning path covers
Module 1, PHP 7 Programming Cookbook, This module 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. It 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 module also incorporates the latest PHP 7.x features.By the end of the module, you will be equipped with the tools and skills required to deliver efficient applications for your websites and enterprises
Module 2, Learning PHP 7 High Performance, This module is fast-paced introduction to PHP 7 will improve your productivity and coding skills. The concepts covered will allow you, as a PHP programmer, to improve the performance standards of your applications. We will introduce you to the new features in PHP 7 and then will run through the concepts of object-oriented programming (OOP) in PHP 7. Next, we will shed some light on how to improve your PHP 7 applications’ performance and database performance. Through this module, you will be able to improve the performance of your programs using the various benchmarking tools discussed in the module. At the end,module discusses some best practices in PHP programming to help you improve the quality of your code
Module 3, Modernizing Legacy Applications in PHP, This module will show you how to modernize your application in terms of practice and technique, rather than in terms of using tools such as frameworks and libraries, by extracting and replacing its legacy artifacts. We will use a step-by-step approach, moving slowly and methodically, to improve your application from the ground up. We’ll show you how dependency injection can replace both the new and global dependencies. We’ll also show you how to change the presentation logic to view files and the action logic to a controller. Moreover, we’ll keep your application running the whole time. Each completed step in the process will keep your codebase fully operational with higher quality. When we are done, you will be able to breeze through your code like the wind. Your code will be autoloaded, dependency-injected, unit-tested, layer-separated, and front-controlled. Most of the very limited code we will add to your application is specific to this module. We will be improving ourselves as programmers, as well as improving the quality of our legacy application.
What you need for this learning path
Module 1:
All you need, to successfully implement the recipes presented in this module will be a computer, 100MB of extra disk space, and a text or code editor (not a word processor!). The first chapter will cover how to set up a PHP 7 development environment. Having a web server is optional as PHP 7 includes a development web server. An Internet connection is not required, but it might be useful to download code (such as the set of PSR-7 interfaces), and review PHP 7.x documentation.
Module 2:
Any hardware specification that is compliant to run the latest versions of the following software should be enough to get through this module:
Operating systems: Debian or Ubuntu
Software: NGINX, PHP 7, MySQL, PerconaDB, Redis, Memcached, Xdebug,
Apache JMeter, ApacheBench, Siege, and Git
Module 3:
You will have refer ‘Chapter 2, Prerequisites’ of this module to understand the basic hardware and software requirements needed on this module. This chapter gives a detailed description of the requirements.
Who this learning path 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
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this course—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the course’s title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a course, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt course, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for this course from your account at http://www.packtpub.com. If you purchased this course elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the course in the Search box.
Select the course for which you’re looking to download the code files.
Choose from the drop-down menu where you purchased this course from.
Click on Code Download.
You can also download the code files by clicking on the Code Files button on the course’s webpage at the Packt Publishing website. This page can be accessed by entering the course’s name in the Search box. Please note that you need to be logged in to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the course is also hosted on GitHub at https://github.com/PacktPublishing/PHP-7-Be-Pro-at-Applications-Development. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our courses—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this course. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your course, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the course in the search field. The required information will appear under the Errata section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this course, you can contact us at <[email protected]>, and we will do our best to address the problem.
Part 1. Module 1
PHP 7 Programming Cookbook
Over 80 recipes that will take your PHP 7 web development skills to the next level!
Chapter 1. Building a Foundation
In this chapter, we will cover the following topics:
PHP 7 installation considerations
Using the built-in PHP web server
Defining a test MySQL database
Installing PHPUnit
Implementing class autoloading
Hoovering a website
Building a deep web scanner
Creating a PHP 5 to PHP 7 code converter
Introduction
This chapter is designed as a quick start that will get you up and running on PHP 7 so that you can start implementing the recipes right away. The underlying assumption for this book is that you already have a good knowledge of PHP and programming. Although this book will not go into detail about the actual installation of PHP, given that PHP 7 is relatively new, we will do our best to point out the quirks and gotchas you might encounter during a PHP 7 installation.
PHP 7 installation considerations
There are three primary means of acquiring PHP 7:
Downloading and installing directly from the source code
Installing pre-compiled binaries
Installing a *AMP package (that is, XAMPP, WAMP, LAMP, MAMP, and so on)
How to do it...
The three methods are listed in order of difficulty. However, the first approach, although tedious, will give you the most finite control over extensions and options.
Installing directly from source
In order to utilize this approach, you will need to have a C compiler available. If you are running Windows, MinGW is a free compiler that has proven popular. It is based on the GNU Compiler Collection (GCC) compiler provided by the GNU project. Non-free compilers include the classic Turbo C compiler from Borland, and, of course, the compiler that is preferred by Windows developers is Visual Studio. The latter, however, is designed mainly for C++ development, so when you compile PHP, you will need to specify C mode.
When working on an Apple Mac, the best solution is to install the Apple Developer Tools. You can use the Xcode IDE to compile PHP 7, or run gcc from a terminal window. In a Linux environment, from a terminal window, run gcc.
When compiling from a terminal window or command line, the normal procedure is as follows:
configure
make
make test
make install
For information on configuration options (that is, when running configure), use the help option:
configure --help
Errors you might encounter during the configuration stage are mentioned in the following table:
Installing PHP 7 from pre-compiled binaries
As the title implies, pre-compiled binaries are a set of binary files that somebody else has kindly compiled from PHP 7 source code and has made available.
In the case of Windows, go to http://windows.php.net/. You will find a good set of tips in the left column that pertain to which version to choose, thread safe versus non-read safe, and so forth. You can then click on Downloads and look for the ZIP file that applies to your environment. Once the ZIP file has been downloaded, extract the files into the folder of your choice, add php.exe to your path, and configure PHP 7 using the php.ini file.
To install the pre-compiled binaries on a Mac OS X system, it is best to involve a package management system. The ones recommended for PHP include the following:
MacPorts
Liip
Fink
Homebrew
In the case of Linux, the packaging system used depends on which Linux distribution you are using. The following table, organized by Linux distribution, summarizes where to look for the PHP 7 package.
Installing a *AMP package
AMP refers to Apache, MySQL, and PHP (also Perl and Python). The * refers to Linux, Windows, Mac, and so on (that is, LAMP, WAMP, and MAMP). This approach is often the easiest, but gives you less control over the initial PHP installation. On the other hand, you can always modify the php.ini file and install additional extensions to customize your installation as needed. The following table summarizes a number of popular *AMP packages:
In the preceding table, we've enlisted the *AMP packages where * is replaced by W for Windows, M for Mac OS X, and L for Linux.
There's more...
When you install a pre-compiled binary from a package, only core extensions are installed. Non-core PHP extensions must be installed separately.
It's worth noting that PHP 7 installation on cloud computing platforms will often follow the installation procedure outlined for pre-compiled binaries. Find out if your cloud environment uses Linux, Mac, or Windows virtual machines, and then follow the appropriate procedure as mentioned in this recipe.
It's possible that PHP 7 hasn't yet reached your favorite repository for pre-compiled binaries. You can always install from source, or consider installing one of the *AMP packages (see the next section). An alternative for Linux-based systems is to use the Personal Package Archive (PPA) approach. Because PPAs have not undergone a rigorous screening process, however, security could be a concern. A good discussion on security considerations for PPAs is found at http://askubuntu.com/questions/35629/are-ppas-safe-to-add-to-my-system-and-what-are-some-red-flags-to-watch-out-fo.
See also
General installation considerations, as well as instructions for each of the three major OS platforms (Windows, Mac OS X, and Linux), can be found at http://php.net/manual/en/install.general.php.
The website for MinGW is http://www.mingw.org/.
Instructions on how to compile a C program using Visual Studio can be found at https://msdn.microsoft.com/en-us/library/bb384838.
Another possible way to test PHP 7 is by using a virtual machine. Here are a couple of tools with their links, which might prove useful:
Vagrant: https://github.com/rlerdorf/php7dev (php7dev is a Debian 8 Vagrant image that is preconfigured for testing PHP apps and developing extensions across many versions of PHP)
Docker: https://hub.docker.com/r/coderstephen/php7/ (it contains a PHP7 Docker container)
Using the built-in PHP web server
Aside from unit testing and running PHP directly from the command line, the obvious way to test your applications is to use a web server. For long-term projects, it would be beneficial to develop a virtual host definition for a web server that most closely mirrors the one used by your customer. Creating such definitions for the various web servers (that is, Apache, NGINX, and so on) is beyond the scope of this book. Another quick and easy-to-use alternative (which we have room to discuss here) is to use the built-in PHP 7 web server.
How to do it...
To activate the PHP web server, first change to the directory that will serve as the base for your code.
You then need to supply the hostname or IP address and, optionally, a port. Here is an example you can use to run the recipes supplied with this book:
cd /path/to/recipes
php -S localhost:8080
You will see output on your screen that looks something like this:
As the built-in web server continues to service requests, you will also see access information, HTTP status codes, and request information.
If you need to set the web server document root to a directory other than the current one, you can use the -t flag. The flag must then be followed by a valid directory path. The built-in web server will treat this directory as if it were the web document root, which is useful for security reasons. For security reasons, some frameworks, such as Zend Framework, require that the web document root is different from where your actual source code resides.
Here is an example using the -t flag:
php -S localhost:8080 -t source/chapter01
Here is an example of the output:
Defining a test MySQL database
For test purposes, along with the source code for the book, we've provided an SQL file with sample data at https://github.com/dbierer/php7cookbook. The name of the database used in the recipes for this book is php7cookbook.
How to do it...
Define a MySQL database, php7cookbook. Also assign rights to the new database to a user called cook with the password book. The following table summarizes these settings:
Here is an example of SQL needed to create the database:
CREATE DATABASE IF NOT EXISTS dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'user'@'%' IDENTIFIED WITH mysql_native_password;
SET PASSWORD FOR 'user'@'%' = PASSWORD('userPassword');
GRANT ALL PRIVILEGES ON dbname.* to 'user'@'%';
GRANT ALL PRIVILEGES ON dbname.* to 'user'@'localhost';
FLUSH PRIVILEGES;
Import the sample values into the new database. The import file, php7cookbook.sql, is located at https://github.com/dbierer/php7cookbook/blob/master/php7cookbook.sql.
Installing PHPUnit
Unit testing is arguably the most popular means of testing PHP code. Most developers will agree that a solid suite of tests is a requirement for any properly developed project. Few developers actually write these tests. A lucky few have an independent testing group that writes the tests for them! After months of skirmishing with the testing group, however, the remains of the lucky few tend to grumble and complain. In any event, any book on PHP would not be complete without at least a nod and a wink towards testing.
The place to find the latest version of PHPUnit is https://phpunit.de/. PHPUnit5.1 and above support PHP 7. Click on the link for the desired version, and you will download a phpunit.phar file. You can then execute commands using the archive, as follows:
php phpunit.phar
Tip
The phar command stands for PHP Archive. The technology is based on tar, which itself was used in UNIX. A phar file is a collection of PHP files that are packed together into a single file for convenience.
Implementing class autoloading
When developing PHP using an object-oriented programming (OOP) approach, the recommendation is to place each class in its own file. The advantage of following this recommendation is the ease of long-term maintenance and improved readability. The disadvantage is that each class definition file must be included (that is, using include or its variants). To address this issue, there is a mechanism built into the PHP language that will autoload any class that has not already been specifically included.
Getting ready
The minimum requirement for PHP autoloading is to define a global __autoload() function. This is a magic function called automatically by the PHP engine when a class is requested but where said class has not been included. The name of the requested class will appear as a parameter when __autoload() is invoked (assuming that you have defined it!). If you are using PHP namespaces, the full namespaced name of the class will be passed. Because __autoload() is a function, it must be in the global namespace; however, there are limitations on its use. Accordingly, in this recipe, we will make use of the spl_autoload_register() function, which gives us more flexibility.
How to do it...
The class we will cover in this recipe is Application\Autoload\Loader. In order to take advantage of the relationship between PHP namespaces and autoloading, we name the file Loader.php and place it in the /path/to/cookbook/files/Application/Autoload folder.
The first method we will present simply loads a file. We use file_exists() to check before running require_once(). The reason for this is that if the file is not found, require_once() will generate a fatal error that cannot be caught using PHP 7's new error handling capabilities:
protected static function loadFile($file)
{
if (file_exists($file)) {
require_once $file;
return TRUE;
}
return FALSE;
}
We can then test the return value of loadFile() in the calling program and loop through a list of alternate directories before throwing an Exception if it's ultimately unable to load the file.
Tip
You will notice that the methods and properties in this class are static. This gives us greater flexibility when registering the autoloading method, and also lets us treat the Loader class like a Singleton.
Next, we define the method that calls loadFile() and actually performs the logic to locate the file based on the namespaced classname. This method derives a filename by converting the PHP namespace separator \ into the directory separator appropriate for this server and appending .php:
public static function autoLoad($class)
{
$success = FALSE;
$fn = str_replace('\\', DIRECTORY_SEPARATOR, $class)
. '.php';
foreach (self::$dirs as $start) {
$file = $start . DIRECTORY_SEPARATOR . $fn;
if (self::loadFile($file)) {
$success = TRUE;
break;
}
}
if (!$success) {
if (!self::loadFile(__DIR__
. DIRECTORY_SEPARATOR . $fn)) {
throw new \Exception(
self::UNABLE_TO_LOAD . ' ' . $class);
}
}
return $success;
}
Next, the method loops through an array of directories we call self::$dirs, using each directory as a starting point for the derived filename. If not successful, as a last resort, the method attempts to load the file from the current directory. If even that is not successful, an Exception is thrown.
Next, we need a method that can add more directories to our list of directories to test. Notice that if the value provided is an array, array_merge() is used. Otherwise, we simply add the directory string to the self::$dirs array:
public static function addDirs($dirs)
{
if (is_array($dirs)) {
self::$dirs = array_merge(self::$dirs, $dirs);
} else {
self::$dirs[] = $dirs;
}
}
Then, we come to the most important part; we need to register our autoload() method as a Standard PHP Library (SPL) autoloader. This is accomplished using spl_autoload_register() with the init() method:
public static function init($dirs = array())
{
if ($dirs) {
self::addDirs($dirs);
}
if (self::$registered == 0) {
spl_autoload_register(__CLASS__ . '::autoload');
self::$registered++;
}
}
At this point, we can define __construct(), which calls self::init($dirs). This allows us to also create an instance of Loader if desired:
public function __construct($dirs = array())
{
self::init($dirs);
}
How it works...
In order to use the autoloader class that we just defined, you will need to require Loader.php. If your namespace files are located in a directory other than the current one, you should also run Loader::init() and supply additional directory paths.
In order to make sure the autoloader works, we'll also need a test class. Here is a definition of /path/to/cookbook/files/Application/Test/TestClass.php:
namespace Application\Test;
class TestClass
{
public function getTest()
{
return __METHOD__;
}
}
Now create a sample chap_01_autoload_test.php code file to test the autoloader:
require __DIR__ . '/../Application/Autoload/Loader.php';
Application\Autoload\Loader::init(__DIR__ . '/..');
Next, get an instance of a class that has not already been loaded:
$test = new Application\Test\TestClass();
echo $test->getTest();
Finally, try to get a fake class that does not exist. Note that this will throw an error:
$fake = new Application\Test\FakeClass();
echo $fake->getTest();
Hoovering a website
Very frequently, it is of interest to scan a website and extract information from specific tags. This basic mechanism can be used to trawl the web in search of useful bits of information. At other times you need to get a list of tags and the SRC attribute, or tags and the corresponding HREF attribute. The possibilities are endless.
How to do it...
First of all, we need to grab the contents of the target website. At first glance it seems that we should make a cURL request, or simply use file_get_contents(). The problem with these approaches is that we will end up having to do a massive amount of string manipulation, most likely having to make inordinate use of the dreaded regular expression. In order to avoid all of this, we'll simply take advantage of an already existing PHP 7 class DOMDocument. So we create a DOMDocument instance, setting it to UTF-8. We don't care about whitespace, and use the handy loadHTMLFile() method to load the contents of the website into the object:
public function getContent($url)
{
if (!$this->content) {
if (stripos($url, 'http') !== 0) {
$url = 'http://' . $url;
}
$this->content = new DOMDocument('1.0', 'utf-8');
$this->content->preserveWhiteSpace = FALSE;
// @ used to suppress warnings generated from // improperly configured web pages
@$this->content->loadHTMLFile($url);
}
return $this->content;
}
Tip
Note that we precede the call to the loadHTMLFile() method with an @. This is not done to obscure bad coding (!) as was often the case in PHP 5! Rather, the @ suppresses notices generated when the parser encounters poorly written HTML. Presumably, we could capture the notices and log them, possibly giving our Hoover class a diagnostic capability as well.
Next, we need to extract the tags which are of interest. We use the getElementsByTagName() method for this purpose. If we wish to extract all tags, we can supply * as an argument:
public function getTags($url, $tag)
{
$count = 0;
$result = array();
$elements = $this->getContent($url)
->getElementsByTagName($tag);
foreach ($elements as $node) {
$result[$count]['value'] = trim(preg_replace('/\s+/', ' ', $node->nodeValue));
if ($node->hasAttributes()) {
foreach ($node->attributes as $name => $attr)
{
$result[$count]['attributes'][$name] =
$attr->value;
}
}
$count++;
}
return $result;
}
It might also be of interest to extract certain attributes rather than tags. Accordingly, we define another method for this purpose. In this case, we need to parse through all tags and use getAttribute(). You'll notice that there is a parameter for the DNS domain. We've added this in order to keep the scan within the same domain (if you're building a web tree, for example):
public function getAttribute($url, $attr, $domain = NULL)
{
$result = array();
$elements = $this->getContent($url)
->getElementsByTagName('*');
foreach ($elements as $node) {
if ($node->hasAttribute($attr)) {
$value = $node->getAttribute($attr);
if ($domain) {
if (stripos($value, $domain) !== FALSE) {
$result[] = trim($value);
}
} else {
$result[] = trim($value);
}
}
}
return $result;
}
How it works...
In order to use the new Hoover class, initialize the autoloader (described previously) and create an instance of the Hoover class. You can then run the Hoover::getTags() method to produce an array of tags from the URL you specify as an argument.
Here is a block of code from chap_01_vacuuming_website.php that uses the Hoover class to scan the O'Reilly website for tags:
// modify as needed
define('DEFAULT_URL', 'http://oreilly.com/');
define('DEFAULT_TAG', 'a');
require __DIR__ . '/../Application/Autoload/Loader.php';
Application\Autoload\Loader::init(__DIR__ . '/..');
// get vacuum
class
$vac = new Application\Web\Hoover();
// NOTE: the PHP 7 null coalesce operator is used
$url = strip_tags($_GET['url'] ?? DEFAULT_URL);
$tag = strip_tags($_GET['tag'] ?? DEFAULT_TAG);
echo 'Dump of Tags: ' . PHP_EOL;
var_dump($vac->getTags($url, $tag));
The output will look something like this:
See also
For more information on DOM, see the PHP reference page at http://php.net/manual/en/class.domdocument.php.
Building a deep web scanner
Sometimes you need to scan a website, but go one level deeper. For example, you want to build a web tree diagram of a website. This can be accomplished by looking for all tags and following the HREF attributes to the next web page. Once you have acquired the child pages, you can then continue scanning in order to complete the tree.
How to do it...
A core component of a deep web scanner is a basic Hoover class, as described previously. The basic procedure presented in this recipe is to scan the target website and hoover up all the HREF attributes. For this purpose, we define a Application\Web\Deep class. We add a property that represents the DNS domain:
namespace Application\Web;
class Deep
{
protected $domain;
Next, we define a method that will hoover the tags for each website represented in the scan list. In order to prevent the scanner from trawling the entire World Wide Web (WWW), we've limited the scan to the target domain. The reason why yield from has been added is because we need to yield the entire array produced by Hoover::getTags(). The yield from syntax allows us to treat the array as a sub-generator:
public function scan($url, $tag)
{
$vac = new Hoover();
$scan = $vac->getAttribute($url, 'href',
$this->getDomain($url));
$result = array();
foreach ($scan as $subSite) {
yield from $vac->getTags($subSite, $tag);
}
return count($scan);
}
Note
The use of yield from turns the scan() method into a PHP 7 delegating generator. Normally, you would be inclined to store the results of the scan into an array. The problem, in this case, is that the amount of information retrieved could potentially be massive. Thus, it's better to immediately yield the results in order to conserve memory and to produce immediate results. Otherwise, there would be a lengthy wait, which would probably be followed by an out of memory error.
In order to keep within the same domain, we need a method that will return the domain from the URL. We use the convenient parse_url() function for this purpose:
public function getDomain($url)
{
if (!$this->domain) {
$this->domain = parse_url($url, PHP_URL_HOST);
}
return $this->domain;
}
How it works...
First of all, go ahead and define the Application\Web\Deep class defined previously, as well as the Application\Web\Hoover class defined in the previous recipe.
Next, define a block of code from chap_01_deep_scan_website.php that sets up autoloading (as described earlier in this chapter):
// modify as needed
define('DEFAULT_URL', 'unlikelysource.com');
define('DEFAULT_TAG', 'img');
require __DIR__ . '/../../Application/Autoload/Loader.php';
Application\Autoload\Loader::init(__DIR__ . '/../..');
Next, get an instance of our new class:
$deep = new Application\Web\Deep();
At this point, you can retrieve URL and tag information from URL parameters. The PHP 7 null coalesce operator is useful for establishing fallback values:
$url = strip_tags($_GET['url'] ?? DEFAULT_URL);
$tag = strip_tags($_GET['tag'] ?? DEFAULT_TAG);
Some simple HTML will display results:
foreach ($deep->scan($url, $tag) as $item) {
$src = $item['attributes']['src'] ?? NULL;
if ($src && (stripos($src, 'png') || stripos($src, 'jpg'))) {
printf('
%s/>', $src);
}
}
See also
For more information on generators and yield from, please see the article at http://php.net/manual/en/language.generators.syntax.php.
Creating a PHP 5 to PHP 7 code converter
For the most part, PHP 5.x code can run unchanged on PHP 7. There are a few changes, however, that are classified as backwards incompatible. What this means is that if your PHP 5 code is written in a certain way, or uses functions that have been removed, your code will break, and you'll have a nasty error on your hands.
Getting ready
The PHP 5 to PHP 7 Code Converter does two things:
Scans your code file and converts PHP 5 functionality that has been removed to its equivalent in PHP 7
Adds comments with //WARNING where changes in language usage have occurred, but where a re-write is not possible
Note
Please note that after running the converter, your code is not guaranteed to work in PHP 7. You will still have to review the //WARNING tags added. At the least, this recipe will give you a good head start converting your PHP 5 code to work in PHP 7.
The core of this recipe is the new PHP 7 preg_replace_callback_array() function. What this amazing function allows you to do is to present an array of regular expressions as keys, with the value representing an independent callback. You can then pass the string through a series of transformations. Not only that, the subject of the array of callbacks can itself be an array.
How to do it...
In a new class Application\Parse\Convert, we begin with a scan() method, which accepts a filename as an argument. It checks to see if the file exists. If so, it calls the PHP file() function, which loads the file into an array, with each array element representing one line:
public function scan($filename)
{
if (!file_exists($filename)) {
throw new Exception(
self::EXCEPTION_FILE_NOT_EXISTS);
}
$contents = file($filename);
echo 'Processing: ' . $filename . PHP_EOL;
$result = preg_replace_callback_array( [
Next, we start passing a series of key/value pairs. The key is a regular expression, which is processed against the string. Any matches are passed to the callback, which is represented as the value part of the key/value pair. We check for opening and closing tags that have been removed from PHP 7:
// replace no-longer-supported opening tags
'!^\<\%(\n| )!' =>
function ($match) {
return '
},
// replace no-longer-supported opening tags
'!^\<\%=(\n| )!' =>
function ($match) {
return '
},
// replace no-longer-supported closing tag
'!\%\>!' =>
function ($match) {
return '?>';
},
Next is a series of warnings when certain operations are detected and there is a potential code-break between how they're handled in PHP 5 versus PHP 7. In all these cases, the code is not re-written. Instead, an inline comment with the word WARNING is added:
// changes in how $$xxx interpretation is handled
'!(.*?)\$\$!' =>
function ($match) {
return '// WARNING: variable interpolation
. ' now occurs left-to-right' . PHP_EOL
. '// see: http://php.net/manual/en/'
. '// migration70.incompatible.php'
. $match[0];
},
// changes in how the list() operator is handled
'!(.*?)list(\s*?)?\(!' =>
function ($match) {
return '// WARNING: changes have been made '
. 'in list() operator handling.'
. 'See: http://php.net/manual/en/'
. 'migration70.incompatible.php'
. $match[0];
},
// instances of \u{
'!(.*?)\\\u\{!' =>
function ($match) {
return '// WARNING: \\u{xxx} is now considered '
. 'unicode escape syntax' . PHP_EOL
. '// see: http://php.net/manual/en/'
. 'migration70.new-features.php'
. '#migration70.new-features.unicode-'
. 'codepoint-escape-syntax' . PHP_EOL
. $match[0];
},
// relying upon set_error_handler()
'!(.*?)set_error_handler(\s*?)?.*\(!' =>
function ($match) {
return '// WARNING: might not '
. 'catch all errors'
. '// see: http://php.net/manual/en/'
. '// language.errors.php7.php'
. $match[0];
},
// session_set_save_handler(xxx)
'!(.*?)session_set_save_handler(\s*?)?\((.*?)\)!' =>
function ($match) {
if (isset($match[3])) {
return '// WARNING: a bug introduced in'
. 'PHP 5.4 which '
. 'affects the handler assigned by '
. 'session_set_save_handler() and '
. 'where ignore_user_abort() is TRUE
. 'has been fixed in PHP 7.'
. 'This could potentially break '
. 'your code under '
. 'certain circumstances.' . PHP_EOL
. 'See: http://php.net/manual/en/'
. 'migration70.incompatible.php'
. $match[0];
} else {
return $match[0];
}
},
Any attempts to use << or >> with a negative operator, or beyond 64, is wrapped in a try { xxx } catch() { xxx } block, looking for an ArithmeticError to be thrown:
// wraps bit shift operations in try / catch
'!^(.*?)(\d+\s*(\<\<|\>\>)\s*-?\d+)(.*?)$!' =>
function ($match) {
return '// WARNING: negative and '
. 'out-of-range bitwise '
. 'shift operations will now
. 'throw an ArithmeticError' . PHP_EOL
. 'See: http://php.net/manual/en/'
. 'migration70.incompatible.php'
. 'try {' . PHP_EOL
. \t
. $match[0] . PHP_EOL
. '} catch (\\ArithmeticError $e) {'
. \t
. 'error_log(File:
. $e->getFile()
. Message:
. $e->getMessage());'
. '}' . PHP_EOL;
},
Note
PHP 7 has changed how errors are handled. In some cases, errors are moved into a similar classification as exceptions, and can be caught! Both the Error and the Exception class implement the Throwable interface. If you want to catch either an Error or an Exception, catch Throwable.
Next, the converter rewrites any usage of call_user_method*(), which has been removed in PHP 7. These are replaced with the equivalent using call_user_func*():
// replaces call_user_method()
with
// call_user_func()
'!call_user_method\((.*?),(.*?)(,.*?)\)(\b|;)!' =>
function ($match) {
$params = $match[3] ?? '';
return '// WARNING: call_user_method() has '
. 'been removed from PHP 7' . PHP_EOL
. 'call_user_func(['. trim($match[2]) . ','
. trim($match[1]) . ']' . $params . ');';
},
// replaces call_user_method_array()
// with call_user_func_array()
'!call_user_method_array\((.*?),(.*?),(.*?)\)(\b|;)!' =>
function ($match) {
return '// WARNING: call_user_method_array()'
. 'has been removed from PHP 7'
. PHP_EOL
. 'call_user_func_array(['
. trim($match[2]) . ','
. trim($match[1]) . '], '
. $match[3] . ');';
},
Finally, any attempt to use preg_replace() with the /e modifier is rewritten using a preg_replace_callback():
'!^(.*?)preg_replace.*?/e(.*?)$!' =>
function ($match) {
$last = strrchr($match[2], ',');
$arg2 = substr($match[2], 2, -1 * (strlen($last)));
$arg1 = substr($match[0],
strlen($match[1]) + 12,
-1 * (strlen($arg2) + strlen($last)));
$arg1 = trim($arg1, '(');
$arg1 = str_replace('/e', '/', $arg1);
$arg3 = '// WARNING: preg_replace() /e
modifier
. 'has been removed from PHP 7'
. PHP_EOL
. $match[1]
. 'preg_replace_callback('
. $arg1
. 'function ($m) { return '
. str_replace('$1','$m', $match[1])
. trim($arg2, '"\'') . '; }, '
. trim($last, ',');
return str_replace('$1', '$m', $arg3);
},
// end array
],
// this is