How to use constructor promotion in PHP 8

This is so good! The only thing that feels wrong is the empty brackets. What is a constructor property promotion? It is a new syntax that PHP 8 brings, allowing two simultaneous operations: Class property declaration and Constructor assignment, right from the constructor. See how it works: See the official documentation.

How to use count() within a loop in PHP? Put it outside

In order to make your code cleaner, the best way is to use the count() function outside of the loop. See the example below: What it count() in PHP? The count() function is a built-in function in PHP that we use to count the number of elements in an array. We can also use it […]

How to use isset() with multiple variables

There is a smart way of using the isset() function in PHP when dealing with several variables. The old-fashioned way: The easiest and smart way to use isset() with multiple variables:

How to fix “trying to get property of non-object” with PHP8 in Laravel

If you’re tired of the error “trying to get property of non-object”, you could use an optional helper method in Laravel. Also, if you’re using PHP8+ you could find the better way of dealing with these issues with the nullsafe operator. Here we go: You can read the official PHP reference on this operator here […]