PHP 7 Programming Blueprints
By Jose Palala and Martin Helmich
()
About this ebook
Related to PHP 7 Programming Blueprints
Related ebooks
Learning PHP 7 High Performance Rating: 0 out of 5 stars0 ratingsPHP 7 Data Structures and Algorithms Rating: 0 out of 5 stars0 ratingsMastering PHP Design Patterns Rating: 0 out of 5 stars0 ratingsLearning PHP Data Objects Rating: 5 out of 5 stars5/5Modular Programming with PHP 7 Rating: 0 out of 5 stars0 ratingsLearning Bootstrap Rating: 1 out of 5 stars1/5Mastering JavaScript Object-Oriented Programming Rating: 0 out of 5 stars0 ratingsMastering JavaScript Design Patterns - Second Edition Rating: 5 out of 5 stars5/5Express Web Application Development Rating: 3 out of 5 stars3/5Laravel 5 Essentials Rating: 0 out of 5 stars0 ratingsMastering Bootstrap 4 Rating: 5 out of 5 stars5/5JavaScript Unlocked Rating: 5 out of 5 stars5/5Mastering Symfony Rating: 0 out of 5 stars0 ratingsJavaScript and JSON Essentials Rating: 5 out of 5 stars5/5RESTful Web API Design with Node.js - Second Edition Rating: 1 out of 5 stars1/5Building Single-page Web Apps with Meteor Rating: 5 out of 5 stars5/5Node.js By Example Rating: 2 out of 5 stars2/5ASP.NET Core 3 and React: Hands-On full stack web development using ASP.NET Core, React, and TypeScript 3 Rating: 0 out of 5 stars0 ratingsGetting Started with React Rating: 0 out of 5 stars0 ratingsClean Code in JavaScript: Develop reliable, maintainable, and robust JavaScript Rating: 5 out of 5 stars5/5React Design Patterns and Best Practices Rating: 0 out of 5 stars0 ratingsBuilding a Web Application with PHP and MariaDB: A Reference Guide Rating: 0 out of 5 stars0 ratingsReact Projects: Build 12 real-world applications from scratch using React, React Native, and React 360 Rating: 0 out of 5 stars0 ratingsReactJS for Jobseekers: The Only Guide You Need to Learn React and Crack Interviews (English Edition) Rating: 0 out of 5 stars0 ratingsASP.NET Web API Security Essentials Rating: 0 out of 5 stars0 ratingsLearning Behavior-driven Development with JavaScript Rating: 5 out of 5 stars5/5Mastering Sass Rating: 0 out of 5 stars0 ratingsPersistence in PHP with Doctrine ORM Rating: 0 out of 5 stars0 ratingsMeteor Design Patterns Rating: 0 out of 5 stars0 ratingsJasmine JavaScript Testing - Second Edition Rating: 0 out of 5 stars0 ratings
Internet & Web For You
No Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsHow to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5Coding For Dummies Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5Podcasting For Dummies Rating: 4 out of 5 stars4/5Six Figure Blogging Blueprint Rating: 5 out of 5 stars5/5Cybersecurity For Dummies Rating: 4 out of 5 stars4/5Get Rich or Lie Trying: Ambition and Deceit in the New Influencer Economy Rating: 0 out of 5 stars0 ratingsHow To Make Money Blogging: How I Replaced My Day-Job With My Blog and How You Can Start A Blog Today Rating: 4 out of 5 stars4/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5More Porn - Faster!: 50 Tips & Tools for Faster and More Efficient Porn Browsing Rating: 3 out of 5 stars3/5The Hacker Crackdown: Law and Disorder on the Electronic Frontier Rating: 4 out of 5 stars4/5Wireless Hacking 101 Rating: 4 out of 5 stars4/5How To Start A Profitable Authority Blog In Under One Hour Rating: 5 out of 5 stars5/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer 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 ratingsWordpress for Beginners: The Easy Step-by-Step Guide to Creating a Website with WordPress Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5How to Destroy Surveillance Capitalism Rating: 4 out of 5 stars4/5How To Start A Podcast Rating: 4 out of 5 stars4/5Create Something Awesome: How Creators are Profiting from Their Passion in the Creator Economy Rating: 0 out of 5 stars0 ratingsSurveillance and Surveillance Detection: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsStop Asking Questions: How to Lead High-Impact Interviews and Learn Anything from Anyone Rating: 5 out of 5 stars5/5The Mega Box: The Ultimate Guide to the Best Free Resources on the Internet Rating: 4 out of 5 stars4/5The Logo Brainstorm Book: A Comprehensive Guide for Exploring Design Directions Rating: 4 out of 5 stars4/5
Reviews for PHP 7 Programming Blueprints
0 ratings0 reviews
Book preview
PHP 7 Programming Blueprints - Jose Palala
Table of Contents
PHP 7 Programming Blueprints
Credits
About the Authors
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Create a User Profile System and use the Null Coalesce Operator
The null coalesce operator
Separation of Concerns
Creating views
Create a profile input form
Admin system
Summary
2. Build a Database Class and Simple Shopping Cart
Building the database abstraction class
Raw query method
Create method
Read method
Select all method
Delete method
Update method
first_of method
last_of method
iterate_over method
searchString method
Using the convert_to_json method to implement a simple API
Shopping Cart
Building the shopping items list
Item template rendering function
Adding checkboxes to the Shopping List page
Cookies in PHP
Building the Checkout page
Thank you page
Installing TCPDF
Admin for managing purchases
Summary
3. Building a Social Newsletter Service
Authentication system
Creating a social login for members
Member dashboard
Marketers dashboard
Administration system for managing marketers
Custom template for our newsletter
Link tracking
AJAX socket chat for support
Introduction to socket.io
Summary
4. Build a Simple Blog with Search Capability using Elasticsearch
Creating the CRUD and admin system
Seeding the post table
What is Elasticsearch?
Installing Elasticsearch and the PHP client
Building a PHP Elasticsearch tool
Adding documents to our Elasticsearch
Querying Elasticsearch
Installing Logstash
Setting up the Logstash configuration
Installing PHP Redis
Encoding and decoding JSON messages
Storing Apache logs in Elasticsearch
Getting filtered data to display with Highcharts
Dashboard app for viewing Elasticsearch logs
Simple search engine with result caching
Cache basics
Cache invalidation of Redis data
Using browser localStorage as cache
Working with streams
Storing and searching XML documents using PHP
Using Elasticsearch to search a social network database
Displaying randomized search engine results
Summary
5. Creating a RESTful Web Service
RESTful basics
REST architectures
Common HTTP methods and response codes
First steps with the Slim framework
Installing Slim
A small sample application
Accepting URL parameters
Accepting HTTP requests with a message body
The PSR-7 standard
Middleware
Implementing the REST service
Designing the service
Bootstrapping the project
Building the persistence layer with MongoDB
Adding and retrieving users
Listing and searching users
Deleting profiles
Validating input
Streams and large files
Profile image upload
Using GridFS storage
Summary
6. Building a Chat Application
The WebSocket protocol
First steps with Ratchet
Architectural considerations
Getting started
Testing WebSocket applications
Playing with the event loop
Implementing the chat application
Bootstrapping the project server-side
Bootstrapping the HTML user interface
Building a simple chat application
Receiving messages
Sending messages
Testing the application
Keeping the connection from timing out
Deployment options
Bridging Ratchet and PSR-7 applications
Accessing your application via the web server
Adding authentication
Creating the login form
Checking the authorization
Connecting users and messages
Summary
7. Building an Asynchronous Microservice Architecture
The target architecture
ZeroMQ patterns
Request/reply pattern
Publish/subscribe pattern
Push/pull pattern
Bootstrapping the project
Building the inventory service
Getting started with ZeroMQ REQ/REP sockets
Using JsonRPC for communication
Making the inventory service multithreaded
Building the checkout service
Using react/zmq
Working with promises
Building the mailing service
Building the shipping service
PUSH/PULL for beginners
Fan-out/fan-in
Bridging ZeroMQ and HTTP
Summary
8. Building a Parser and Interpreter for a Custom Language
How interpreters and compilers work
Languages and grammars
Your first PEG parser
Evaluating expressions
Building an Abstract Syntax Tree
Building a better interface
Evaluating variables
Adding logical expressions
Comparisons
The and
and or
operators
Conditions
Working with structured data
Working with objects
Optimizing the interpreter by adding a compiler
Verifying performance improvements
Summary
9. Reactive Extensions in PHP
An introduction to observables
Introduction to event loop and ReactiveX
delay
defer
Scheduler
recursive-scheduler
map and flatMap
reduce
toArray
merge
do
scan
zip
Parsing logs through a Reactive scheduler
Event queues with ReactiveX
Summary
PHP 7 Programming Blueprints
PHP 7 Programming Blueprints
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book 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 book to ensure the accuracy of the information presented. However, the information contained in this book 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 book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2016
Production reference: 1160916
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-971-4
www.packtpub.com
Credits
About the Authors
Jose Palala has been working professionally with PHP for at least 8 years. He has experience working with PHP frameworks such as Eden PHP, CodeIgniter, Laravel and Zend.
He has worked for Philippine-based IT companies for at least 8 years, working on projects ranging from internal corporate systems and CMS websites. In his spare time, he regularly contributes back to the tech community in the Philippines.
I would like to thank everyone at Packt Publishing, it’s been great working with them since Day 1. Super thanks to all to my colleagues, friends and family who have helped me to become a better developer and have helped me become what I am today.
Martin Helmich holds a Master's degree in Computer Science from the University of Applied Sciences in Osnabrück. He works as a software architect and specializes in building distributed applications using web technologies and Microservice Architectures. Besides programming in Go, PHP, Python and Node.JS, he also builds infrastructures using configuration management tools like SaltStack and container technologies like Docker.
He is an open source enthusiast and likes to make fun of people who are not using Linux. In his free time, you'll probably find him coding on one of his open source pet projects, listening to music or reading science-fiction literature.
About the Reviewer
Shuvankar Sarkar is an IT analyst and experienced in C#, .NET, PHP and web development.
He is a technology enthusiast and maintains his blog at http://shuvankar.com/. You can follow him on Twitter at @sonu041. He is interested in computer security as well.
I would like to thank my family for making my life easier and full of happiness.
www.PacktPub.com
eBooks, discount offers, and more
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.
At www.PacktPub.com , you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Preface
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, which provides major backward-compatibility breaks and focuses on providing improved performance and speed. With the rise in demand for high performance, this newest version contains everything you need to build efficient applications. PHP 7 provides improved engine execution, better memory usage, and a better set of tools allowing you to maintain high traffic on your websites with low-cost hardware and servers through a multithreading web server.
What this book covers
Chapter 1, Create a User Profile System and use the Null Coalesce Operator, we'll discover new PHP 7 features and build app for storing user profiles.
Chapter 2, Build a Database Class and Simple Shopping Cart, we'll create a simple database layer library which will help us access our database. We'll cover some tips on making our queries secure, and how to make our coding simpler and more succinct with PHP 7.
Chapter 3, Building a Social Newsletter Service, we'll be building a social newsletter service, which will have a way for users to sign in using their social login and allow them to register to a newsletter. We'll also make a simple admin system for managing the newsletters.
Chapter 4, Build a Simple Blog with Search Capability using Elasticsearch, you will learn how to create a blog system, experiment with ElasticSearch and how to apply it in your code. Also, you will learn how to create a simple blog application and store data into MySQL.
Chapter 5, Creating a RESTful Web Service, shows you how create a RESTful web service that can be used to manage user profiles. The service will be implemented using the Slim micro framework and use a MongoDB database for persistence. The chapter also covers the basics of RESTful web services, most importantly the common HTTP request and response methods, the PSR-7 standard and PHP 7’s new mongodb extension.
Chapter 6, Building a Chat Application, describes the implementation of a real-time chat application using WebSockets. You will learn how to use the Ratchet framework to build stand-alone WebSocket and HTTP servers with PHP and how to connect to WebSocket servers in a JavaScript client application. We will also discuss how you can implement authentication for WebSocket applications and how to deploy them in a production environment.
Chapter 7, Building an Asynchronous Microservice Architecture, covers the implementation of a (small) microservice architecture. Instead of RESTful web services, you will use ZeroMQ in this chapter for network communication, an alternative communication protocol that focuses on asynchronicity, loose coupling and high performance.
Chapter 8, Building a Parser and Interpreter for a Custom Language, describes how to use the PHP-PEG library to define a grammar and implement a parser for a custom expression language that can be used to add end-user development features to enterprise applications.
Chapter 9, Reactive Extensions in PHP, here we'll look into the Reactive extensions library for PHP, and and try to build a simple scheduled app.
What you need for this book
You’ll need to download and install PHP 7 from the official PHP website. You’ll also need to install a Webserver such as Apache or Nginx configured to run PHP 7 by default.
If you are experienced with virtual machines, you can also use Docker containers and/or Vagrant to build an environment with PHP 7 installed.
Who this book is for
The book is for web developers, PHP consultants, and anyone who is working on multiple projects with PHP. Basic knowledge of PHP programming is assumed.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: Let's create a simple UserProfile class.
A block of code is set as follows:
function fetch_one($id) {
$link = mysqli_connect('');
$query = SELECT * from
. $this->table . WHERE `id` ='
. $id '
;
$results = mysqli_query($link, $query);
}
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
'credit_card' => $credit_card, 'items' => //
'total' => $total,
Any command-line input or output is written as follows:
mysql> source insert_profiles.sql
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: Simply click on Allow access and then click on OK.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—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 feedback@packtpub.com, and mention the book'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 book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, 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 book 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 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 book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book'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 book is also hosted on GitHub at https://github.com/PacktPublishing/PHP-7-Programming-Blueprints. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Downloading the color images of this book
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/PHP7ProgrammingBlueprints_ColorImages.pdf.
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 books—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 book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, 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 book 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 copyright@packtpub.com 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 book, you can contact us at questions@packtpub.com, and we will do our best to address the problem.
Chapter 1. Create a User Profile System and use the Null Coalesce Operator
To begin this chapter, let's check out the new null coalesce in PHP 7. We'll also learn how to build a simple profiles page with listed users that you can click on, and create a simple CRUD-like system which will enable us to register new users to the system and delete users for banning purposes.
We'll learn to use the PHP 7 null coalesce operator so that we can show data if there is any, or just display a simple message if there isn't any.
Let's create a simple UserProfile class. The ability to create classes has been available since PHP 5.
A class in PHP starts with the word class, and the name of the class:
class UserProfile {
private $table = 'user_profiles';
}
}
We've made the table private and added a private variable, where we define which table it will be related to.
Let's add two functions, also known as a method, inside the class to simply fetch the data from the database:
function fetch_one($id) {
$link = mysqli_connect('');
$query = SELECT * from
. $this->table . WHERE `id` ='
. $id '
;
$results = mysqli_query($link, $query);
}
function fetch_all() {
$link = mysqli_connect('127.0.0.1', 'root','apassword','my_dataabase' );
$query = SELECT * from
. $this->table . ";
$results = mysqli_query($link, $query);
}
The null coalesce operator
We can use PHP 7's null coalesce operator to allow us to check whether our results contain anything, or return a defined text which we can check on the views, this will be responsible for displaying any data.
Let's put this in a file which will contain all the define statements, and call it:
//definitions.php
define('NO_RESULTS_MESSAGE', 'No results found');
require('definitions.php');
function fetch_all() {
...same lines ...
$results = $results ?? NO_RESULTS_MESSAGE;
return $message;
}
On the client side, we'll need to come up with a template to show the list of user profiles.
Let's create a basic HTML block to show that each profile can be a div element with several list item elements to output each table.
In the following function, we need to make sure that all values have been filled in with at least the name and the age. Then we simply return the entire string when the function is called:
function profile_template( $name, $age, $country ) {
$name = $name ?? null;
$age = $age ?? null;
if($name == null || $age === null) {
return 'Name or Age need to be set';
} else {
return '
';
}
}
Separation of Concerns
In a proper MVC architecture, we need to separate the view from the models that get our data, and the controllers will be responsible for handling business logic.
In our simple app, we will skip the controller layer since we just want to display the user profiles in one public facing page. The preceding function is also known as the template render part in an MVC architecture.
While there are frameworks available for PHP that use the MVC architecture out of the box, for now we can stick to what we have and make it work.
PHP frameworks can benefit a lot from the null coalesce operator. In some codes that I've worked with, we used to use the ternary operator a lot, but still had to add more checks to ensure a value was not falsy.
Furthermore, the ternary operator can get confusing, and takes some getting used to. The other alternative is to use the isSet function. However, due to the nature of the isSet function, some falsy values will be interpreted by PHP as being a set.
Creating views
Now that we have our model complete, a template render function, we just need to create the view with which we can look at each profile.
Our view will be put inside a foreach block, and we'll use the template we wrote to render the right values:
//listprofiles.php
stylesheet href=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css
>
foreach($results as $item) {
echo profile_template($item->name, $item->age, $item->country;
}
?>
Let's put the code above into index.php .
While we may install the Apache server, configure it to run PHP, install new virtual hosts and the other necessary features, and put our PHP code into an Apache folder, this will take time. So, for the purposes of testing this out, we can just run PHP's server for development.
To run the built-in PHP server (read more at http://php.net/manual/en/features.commandline.webserver.php ) we will use the folder we are running, inside a terminal:
php -S localhost:8000
If we open up our browser, we should see nothing yet, No results found. This means we need to populate our database.
If you have