Book Image

Modular Programming with PHP 7

By : Branko Ajzele
Book Image

Modular Programming with PHP 7

By: Branko Ajzele

Overview of this book

Modular design techniques help you build readable, manageable, reusable, and more efficient codes. PHP 7, which is a popular open source scripting language, is used to build modular functions for your software. With this book, you will gain a deep insight into the modular programming paradigm and how to achieve modularity in your PHP code. We start with a brief introduction to the new features of PHP 7, some of which open a door to new concepts used in modular development. With design patterns being at the heart of all modular PHP code, you will learn about the GoF design patterns and how to apply them. You will see how to write code that is easy to maintain and extend over time with the help of the SOLID design principles. Throughout the rest of the book, you will build different working modules of a modern web shop application using the Symfony framework, which will give you a deep understanding of modular application development using PHP 7.
Table of Contents (19 chapters)
Modular Programming with PHP 7
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Free Chapter
1
Ecosystem Overview
Index

Preface

Building modular applications is a challenging task. It involves a wide spectrum of knowledge, ranging from design patterns and principles to the ins and outs of the chosen technology stack. The PHP ecosystem has quite a selection of tools, libraries, frameworks, and platforms to assist us with our goal of modular application development.

PHP 7 brings a lot of improvements that can further assist achieving that goal. We will start our journey by looking into some of these improvements. By the end of this book, our final delivery will be a modular web shop application built by the Symfony framework.

What this book covers

Chapter 1, Ecosystem Overview, gives a gentle introduction to the current state of the PHP ecosystem. It looks into the latest features of PHP 7, some of which open a door to the new concepts of use in modular development. Furthermore, this chapter glosses over the popular PHP frameworks.

Chapter 2, GoF Design Patterns, describes recurring solutions to common problems in software design. Practical PHP examples are given for each of the following patterns: creation pattern types, structural patterns, and behavioral patterns.

Chapter 3, SOLID Design Principles, dives into the five basic principles of object-oriented programming and design under the acronym SOLID (single responsibility, open-closed, Liskov substitution, Interface Segregation, and dependency inversion).It gives practical examples and explains the importance of these principles in modular development.

Chapter 4, Requirement Specification for a Modular Web Shop App, guides a reader through the process of defining overall application requirements. It starts by defining actual application feature requirements and progresses all the way to the technology stack selection.

Chapter 5, Symfony at a Glance, gives a high-level overview of Symfony as a framework, a set of tools, and a development methodology. It focuses on the building blocks that we will need to build our modular application.

Chapter 6, Building the Core Module, guides you through setting up a core module based on the Symfony bundle. The core module is then used to set the structure and dependencies for other modules to use.

Chapter 7, Building the Catalog Module, guides us through building a self-sufficient module that matches the web shop catalog-only feature set. It shows us how to set up entities relevant to the module functionality and how to manage those entities and their interactions using the existing framework.

Chapter 8, Building the Customer Module, guides us through building a self-sufficient module that matches the web shop customer-related feature set. It shows us how to set up entities relevant to the module's functionality and how to manage those entities and their interactions using the existing framework. It further shows us how to create a register and login systems.

Chapter 9, Building the Payment Module, guides us through building a self-sufficient module that matches the web shop payment-related feature set. It shows us how to integrate with a third-party payment provider. It further shows us how to expose a payment provider as service for other modules to use.

Chapter 10, Building the Shipment Module, guides us through building a self-sufficient module that matches the web shop shipment-related feature set. It shows us how to define several flat methods that yield different shipment pricing based on various cart product attributes. It further shows us how to expose a shipment method as service for other modules to use.

Chapter 11, Building the Sales Module, guides us through building a self-sufficient module that matches the web shop sales-only feature set. It shows us how to set up cart, cart item, order, and order item entities relevant to the module functionality and how to manage those entities and their interactions using the existing framework.

Chapter 12, Integrating and Distributing Modules, integrates all the modules built in the preceding chapters into a single functioning application. Moving on, it guides us through the modern PHP module distribution techniques. These include Git and Composer, which in turn indirectly include GitHub and Packagist.

What you need for this book

In order to successfully run all the examples provided in this book, you will need either your own web server or a third-party web-hosting solution. The high-level technology stack includes PHP 7.0 or greater, Apache/Nginx, and MySQL.

The Symfony framework itself comes with a detailed list of system requirements that can be found at http://symfony.com/doc/current/reference/requirements.html.

This book assumes that the reader is familiar with setting up the complete development environment.

Who this book is for

This book is primarily intended for intermediate-level PHP developers, with little to no knowledge of modular programming who want to understand design patterns and principles in order to better utilize the existing framework for modular application development.

The modular web-shop application developed as a part of this book uses the Symfony framework. However, no previous knowledge of the Symfony framework is assumed or required.

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: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

function hint (int $A, float $B, string $C, bool $D)
{
    var_dump($A, $B, $C, $D);
}

Any command-line input or output is written as follows:

sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
sudo chmod a+x /usr/local/bin/symfony

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: "Clicking the Next button moves you to the next screen."

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 , 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/Modular-Programming-with-PHP7. 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/ModularProgrammingwithPHP7_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 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 , and we will do our best to address the problem.