Book Image

PHP 7 Programming Blueprints

By : Jose Palala, Martin Helmich
Book Image

PHP 7 Programming Blueprints

By: Jose Palala, Martin Helmich

Overview of this book

When it comes to modern web development, performance is everything. The latest version of PHP has been improvised and updated to make it easier to build for performance, improved engine execution, better memory usage, and a new and extended set of tools. If you’re a web developer, what’s not to love? This guide will show you how to make full use of PHP 7 with a range of practical projects that will not only teach you the principles, but also show you how to put them into practice. It will push and extend your skills, helping you to become a more confident and fluent PHP developer. You’ll find out how to build a social newsletter service, a simple blog with a search capability using Elasticsearch, as well as a chat application. We’ll also show you how to create a RESTful web service, a database class to manage a shopping cart on an e-commerce site and how to build an asynchronous microservice architecture. With further guidance on using reactive extensions in PHP, we’re sure that you’ll find everything you need to take full advantage of PHP 7. So dive in now!
Table of Contents (15 chapters)
PHP 7 Programming Blueprints
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
4
Build a Simple Blog with Search Capability using Elasticsearch

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' => //<all the items and prices>//, 
'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 [email protected], 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:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. 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 [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 book, you can contact us at [email protected], and we will do our best to address the problem.