PHP - Splat Operator

Since PHP 5.6, you can use splat operator (...) to create simpler variadic functions (functions that take an undefined number of arguments). To demonstrate this, the code snippet below provides a function that will return the sum of any number of numbers: function adder(...$numbers) { $sum = 0; foreach ($numbers as…

Read this article

PHP 7 - Spaceship Operator

The spaceship operator ( <=> ) is new in PHP 7.0. You may hear it being referred to as the "Combined Comparison Operator" and sometimes the "rocket" or "rocketship" operator as it also looks like a rocket on its side. Simply put, it will return a -1, 0, or 1…

Read this article

PHP - Generators

Generators have been available since PHP 5.5 and the currently minimum supported (security) version of PHP is 5.6 so you should definitely have access to them. Generators are a simple way to implement iterators which can be used in foreach statements. You may be familiar with the range…

Read this article

PHP 7.0 - Anonymous Classes

A new feature of php 7.0 is anonymous classes. Anonymous classes are useful when simple, one-off objects need to be created, such as a value object. Below is an example of using an anonymous class for ensuring a user provided string is valid, and converts it for later use.…

Read this article

Using DI to Avoid Inheritance

I often find myself using inheritance to try and DRY up my codebase. However, that can easily lead to inheritance hell, which you know you are in when: You have to implement methods you don't want/need in child classes. You enter the "great" territory with great-grandparent and great-great-grandparent classes.…

Read this article

Ubuntu 16.04 - Install GPG Extension for PHP 7

Below is a script you can run to install the GPG extension for the PHP 7.0 CLI on Ubuntu 16.04. It may work on other distributions and versions of PHP, but I have not tested that. Related Posts Debian 8 - Install GPG Extension for PHP 5.6…

Read this article

Extremely Defensive PHP

Below is a talk from Marco (Ocramius) on extremely defensive PHP. Marco works at a company called Roave (software consultancy), and is an active contributor in th Zend Framework and Doctrine teams. He conveys a lot of great points that I think we could all benefit from. Introduction to speaker…

Read this article

PHP-FPM - Show/Hide Errors

Turning on the display of errors with PHP-FPM is as easy as switching display_errors from Off to On in the /etc/php/7.0/fpm/php.ini file. To make life easier, you can copy/execute the following scripts to do this for you. Display Errors (Dev) SEARCH="display_…

Read this article

Ubuntu 16.04 - Install NGINX with PHP

This tutorial will quickly get you started with deploying an NGINX webserver and configuring it to work with using PHP 7.0. If you want to install MySQL, MariaDB, or PostgreSQL as well, that's easy enough to do separately after/before completing this tutorial. Steps Install NGINX and php-fpm sudo…

Read this article

PHP - Using Traits

Introduction Traits, which were introduced in PHP 5.4, are defined as: Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent…

Read this article