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. Let’s create an example where we can convert any string to a slug. We’ll have 2 methods so that we can pass either a string or iterable data. […]

Read More »

Setting Visibility For Your Class Constant’s 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. Any constant’s within your class set to protected or private that you try to access directly outside of the class will throw a Fatal Error. Example Usage <?php class […]

Read More »

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. Let’s review a Notifier interface that we may have. We can easily create a anonymous implementation by using […]

Read More »

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. Let’s look at some examples. Basic Example <?php $names = [ 'Frank Perez', 'Carmella Osman', […]

Read More »

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 with out the need of returning any values. Functions or Methods declared with void as their return type would simply use an empty return statement […]

Read More »

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. Let’s look at an example where we setup a simple product class […]

Read More »

PHP Group Multiple use Declarations

As of PHP 7, you can now group your imported classes, functions, and constants from under the same namespace. Let’s look at a example to show you how you can start using this today in your projects. Previously you’d have to write out your imports like so:

Now you can group them since they […]

Read More »

PHP Null Coalescing Operator

One of my new favorite additions to PHP 7, is the Null Coalesce 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. I think the best way to show you this example, is maybe […]

Read More »