Reviewing Property Declaration In PHP

Reviewing Property Declaration In PHP Properties are a foundation part of classes. They are declared with their visibility, and their name. Then, they might be augmented with a default value, a type (even several), and may be also options like static or readonly. With hierarchies and large families of classes, we shall start reviewing property […]

The very useful variadic argument

Remove static variables One discreet update of PHP 8.1 is the upgrade of PHP static variables behavior in classes : Static variables in methods inheritance. Let’s review what are static variables in PHP, why they were problematic so far, and strategies to upgrade the code to remove this problem. PHP static variables While static properties […]

Adoption of PHP 8 attributes in 2022

Adoption of PHP 8 attributes in 2022 PHP 8 introduced the attributes, in lieu of the phpdoc comments. They may be added to classes, methods, functions, parameters, properties and class constants and provide a PHP-way to write custom configurations. This is a great addition for the static analysis tools, and PHP in general. 18 months […]

Move that foreach() inside the method

loops goes inside

Move that foreach() inside the method Several PHP functions work with single or multiple values. For example, array_merge(), which merges several arrays into one. Or, isset(), which checks simultaneously the existence of several variables. str_replace() and preg_replace_callback() also come to mind. This is a good place to move that foreach() inside the method. This approach […]

Literal candidates for constants

Literal candidates for constants Literals are hard coded values in the source. Once they are written, they can’t be changed without altering the source code. This is always fine, initially, while building the first implementations. Yet, later, it may arise that such literal have to be managed from different points of the code. This is […]

Interfaces are not class templates

tagged on a cow

Interfaces are not class templates Interfaces are an important part of OOP, in PHP as in other OOP languages. According to the manual, “Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are implemented.” Besides interfaces, PHP also supports abstract classes, which […]

One less hidden bug with Enum

potato potato

One less hidden bug with Enum PHP 8.1 provides now enumerations, a.k.a. enums, as a native PHP construct. They provide a custom type with a limited set of values. Enumerations have been around in PHP in various forms, for a long time : check bensampo/laravel-enum, spatie/enum, marc-mabe/php-enum, eloquent/enumeration or even any framework (CakePHP-enums). Before that, […]

6 unicity traps that sideline PHP code

Thorny issues

6 unicity traps that sideline PHP code Sometimes, code is being ignored by PHP : code is painstakingly written but never gets executed. How is that even possible? May be, because it was not so painstakingly coded. Indeed, there are some PHP coding structures which natively and silently overwrites or ignore code. This leads to […]