PHP
PHP articles and tutorials ranging from new language features to using interesting packages.
SOLID Principles in Modern PHP
SOLID has not changed in years, but PHP has. Here are the five object-oriented design principles rewritten for PHP 8.5, using typed properties, readonly, enums, constructor promotion, and property hooks to express the same ideas with far less boilerplate.
What's New in PHP 8.5
PHP 8.5 leans into composition and ergonomics. Here are its headline features with practical examples: the pipe operator, cloning with property updates, the NoDiscard attribute, array_first and array_last, the new URI extension, and backtraces on fatal errors.
What's New in PHP 8.4
PHP 8.4 brought one of the biggest syntax additions of the 8.x line. Here are its headline features with practical examples: property hooks, asymmetric visibility, new without parentheses, the array_find family, the Deprecated attribute, and a modern HTML5 DOM parser.
What's New in PHP 8.3
PHP 8.3 is a focused release full of quality-of-life wins. Here are its headline features with practical examples: typed class constants, the Override attribute, json_validate, dynamic constant fetch, random string generation, and readonly deep cloning.
What's New in PHP 8.2
PHP 8.2 polished the type system and the immutability story. Here are its headline features with practical examples: readonly classes, DNF types, standalone null, false and true types, the new Random extension, constants in traits, and sensitive parameter redaction.
What's New in PHP 8.1
PHP 8.1 is one of the most loved releases of the 8.x line. Here are its headline features with practical examples: enums, readonly properties, first-class callable syntax, fibers, the never return type, and new in initializers.
What's New in PHP 8.0
PHP 8.0 was a true major version. Here is a tour of its headline features with practical examples: constructor property promotion, named arguments, the match expression, the nullsafe operator, union types, and string helpers that finally read like English.
FizzBuzz in PHP: A Fresh Approach
FizzBuzz is a very popular programming question that tests your logic to see if you can build a simple program.
PHP's array_reduce is not only for outputting single values
PHP's array_reduce is a simple way to partition a set of data or return a single value. It is super powerful and worth spending time learning.
Support for keys in list(), or its new shorthand syntax [] in PHP
Now as of PHP 7.1, you can define the keys of your array that will be parsed when destructuring your arrays. Prior to PHP 7.1, you could only use arrays with numeric indexes. Now with this new addition, our lives just got easier.
Type Hinting With The Iterable pseudo-type In PHP
As of PHP 7.1, you can now type hint your method/function arguments with the keyword iterable for handling arrays or even objects that implement the Traversable interface.
Type Hinting Callable Functions in PHP
As of PHP 5.4, you can type hint your method arguments with the callable keyword allowing you to enforce the type of data that is passed via your arguments.
Setting Visibility for Your Class Constants in PHP
Now in PHP 7.1+, you can set different visibility modifiers for each of your class constants. The available visibility modifiers consist of public, protected, and private.
Using Anonymous Classes in PHP
As of PHP 7, you can now create quick throwaway objects for use within your projects. This can be especially useful for your automated tests, for instance, with allowing you to create quick implementations of your interfaces.
Symmetric Array Destructuring in PHP
As of PHP 7.1, you can now use the shorthand array syntax to destructure your arrays for assignment. Previously you would have had to use a function like list, but now you can use the simple new array shorthand syntax.
Using PHP's array_map to format your arrays without loops
So let's face it, loops are a bit boring. So how can we mix it up? Let's assume we have a case where we have a CSV file that we want to quickly parse.
SOLID Principles in PHP
The 5 basic principles for Object-Oriented Design, SOLID, were first created in an effort to improve maintainability in our code bases. SOLID is a mnemonic acronym that stands for each of the following principles: Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion.
Filtering Arrays Without Using Loops in PHP
PHP has a built-in function called array_filter that allows you to filter through your arrays without the need for a loop. Personally, this approach feels much cleaner to me and simpler to comprehend.
Void Return Types in PHP
As of PHP 7.1, we can now use void return types within our methods. This is useful for cases where you have methods that are just setting or processing data without the need of returning any values.
Type Hinting with Nullable Types in PHP
As of PHP 7.1, you can now set your type declarations as nullable by simply prefixing them with a question mark ?. In doing so a null value can be passed in as a parameter or returned as a value for your methods.
PHP Group Multiple use Declarations
As of PHP 7, you can now group your imported classes, functions, and constants from under the same namespace.
PHP Null Coalescing Operator
One of my new favorite additions to PHP 7, is the Null Coalescing Operator. It cleans up your code by removing a tedious step of checking if some value is isset() and not NULL and returning it or if not setting a default.
PHP Spaceship Operator
One of the new features to hit PHP 7 is the Spaceship Operator. This new trick helps improve the way you'd compare 2 expressions. In short, the comparison returns 1 of 3 values (-1, 0, or 1) depending on the result of the comparison.
Return Type Declarations in PHP
PHP 7 now makes it possible to declare return types for your methods. This allows you better control over the data that will be returned from each method in your application.
Scalar Type Hints in PHP
Starting with PHP 7.0, it's now possible to declare scalar type hints for your method arguments. Previously, we were able to use array and callable, but now with PHP 7+, we have much more control.