Discover how to apply the principles of OOP, types, and package management the right way
Key Features
- Explore the latest PHP 8.1 features and modern web development practices
- Refactor your legacy codebase using modern features and styles in PHP
- Create your own Composer packages that can be reused across all PHP projects
Book Description
PHP has come a long way since its introduction. While the language has evolved with PHP 8, there are still a lot of websites running on a version of PHP that is no longer supported. If you are a PHP developer working with legacy PHP systems and want to discover the tenants of modern PHP, this is the book for you.
The Art of Modern PHP 8 walks you through the latest PHP features and language concepts. The book helps you upgrade your knowledge of PHP programming and practices. Starting with object-oriented programming (OOP) in PHP and related language features, you'll work through modern programming techniques such as inheritance, understand how it contrasts with composition, and finally look at more advanced language features. You'll learn about the MVC pattern by developing your own MVC system and advance to understanding what a DI container does by building a toy DI container. The book gives you an overview of Composer and how to use it to create reusable PHP packages. You'll also find techniques for deploying these packages to package libraries for other developers to explore.
By the end of this PHP book, you'll have equipped yourself with modern server-side programming techniques using the latest versions of PHP.
What you will learn
- Understand how to use modern PHP features such as objects, types, and more
- Get to grips with PHP package management using Composer
- Explore dependency injection for your PHP applications
- Find out what the differences are between legacy and modern PHP 8 code and practices
- Write clean PHP code and implement design patterns
- Get hands-on with modern PHP using examples applying MVC and DI techniques
Who this book is for
The book is for existing PHP developers and CTO-level professionals who are working with PHP technologies, including legacy PHP, in production. The book assumes beginner-level knowledge of PHP programming as well as experience with server-side development.
Table of Contents
- Object-Oriented PHP
- Inheritance and Composition, Encapsulation and Visibility, Interfaces and Concretions
- Advanced OOP Features
- Scalar, Arrays, and Special Types
- Object Types, Interfaces, and Unions
- Parameter, Property, and Return Types
- Design Patterns and Clean Code
- Model, View, Controller (MVC) Example
- Dependency Injection Example
- Composer For Dependencies
- Creating Your Own Composer Package
- The Awesomeness That Is 8.1
Author(s): Joseph Edmonds
Publisher: Packt Publishing
Year: 2021
Language: English
Commentary: True PDF
Pages: 420
Cover
Title
Copyright and Credits
Foreword
Table of Contents
Section 1 – PHP 8 OOP
Chapter 1: Object-Oriented PHP
What is OOP?
Understanding the phrase
OOP alternatives
Learning object-oriented PHP
PHP OOP basics
Classes
Interfaces
Namespaces
Summary
Chapter 2: Inheritance and Composition, Encapsulation and Visibility, Interfaces and Concretions
Encapsulation and visibility
Private, protected, and public
Inheritance–the classical parent, child, and "is a" style
Constructor promotion
Composition–the modern, flexible "has a" style
Interfaces instead of abstract classes
Summary
Chapter 3: Advanced OOP Features
SPL – the Standard PHP Library
Data structures
Iterators
Exceptions and error handling
Yoda and Pokemon exception handling
Exception and error handling best practices
Meta-programming with reflection and attributes
Reflection
Attributes
Summary
Section 2 – PHP Types
Chapter 4: Scalar, Arrays, and Special Types
Scalar types
Strings
Ints
Floats
Bools
Type juggling
Type and value comparison
Arrays and iterables
Arrays
Iterables
DocBlock types
Nullable, void, and uninitialised
Null and nullable
Void
Uninitialised
Resources
Summary
Chapter 5: Object Types, Interfaces, and Unions
Object type inheritance
Object comparison
Passing objects by reference
Making new objects
Summary
Chapter 6: Parameter, Property, and Return Types
Coercive and strict modes
Covariance and contravariance
Contravariance – that is, allowing less specific parameter types than the parent
Covariance – that is, allowing more specific return types than the parent
Remembering the rules
Summary
Section 3 – Clean PHP 8 Patterns and Style
Chapter 7: Design Patterns and Clean Code
Clean Code Acronyms (CCA)
DRY
KICK
Design patterns
Creational patterns
Structural patterns
Behavioral patterns
Architectural patterns
Summary
Chapter 8: Model, View, Controller (MVC) Example
Model, View, Controller – MVC
Controller
Front controller
Model
Entity pattern
Universally unique identifier (UUID)
Persistence and ORM
Singleton anti-pattern
Repository pattern
Collection pattern
View
It lives
Summary
Chapter 9: Dependency Injection Example
Inversion of control container
Dependency injection and the service locator
The service locator anti-pattern
Summary
Chapter 10: Composer For Dependencies
What is Composer?
What are dependencies?
Dependency resolution
But wait, there's more
In a nutshell, it's awesome
Get it installed!
Composer autoloader
Old-fashioned versus autoloaded
A real-world, old-fashioned example
Extra points
Autoloading in PHP
Configuring Composer autoloader
Using Composer to require packages
Finding dependencies
Initialisation of composer.json
What is a vendor?
Minimum stability
Package type
License
So far, so good
Installing monolog
Ignore the vendor directory in your VCS
The composer.lock file
Adding extra dependencies
Version constraints
Semver in a nutshell
Caret (^) Version Range
Tilde (~) Version Range
Exact version
Version range
Multiple acceptable versions
Full documentation
Dev and prod dependencies
Requiring dev dependencies
Installing without dev dependencies
Summary
Chapter 11: Creating Your Own Composer Package
Writing composer.json
Name
Description
Keywords
License
Using source folders and namespaces
Executable Binaries Configured under bin
Extensionless files
Executable permissions
Finding an autoloader
Using versions and tagging
Creating a Git tag
SemVer
Stable and development versions
Private Git repos as Composer dependencies
Publishing on Packagist
Things to check before you publish
Submitting your package to Packagist
Created
If you were just testing, delete it
Summary
Section 5 – Bonus Section - PHP 8.1
Chapter 12: The Awesomeness That Is 8.1
Enumerations
Basic and backed enums
Creating enum instances
Not just classes with cases
Readonly properties
Readonly gotchas
Intersection types
Never return type
Final constants
Summary – end of the book
Other Books You May Enjoy
Index