

Part 1 and part 2 of the article were published in this site.ĭespite traits were added to PHP 5.4, it took a while for the PHP community to start enjoying and producing reusable code based on traits. Last year there was even one article by Dave Smith that explained how to apply traits in practice in your PHP developers. Traits can be used to add common functionality to classes for unrelated purposes. Traits avoid the need to do copy and pasting of parts of code that may be reused in many classes, despite traits code does not form a class just by itself.

Traits allow a different kind of reuse provided by traditional Object Oriented Languages. Extreme Code Reuse with TraitsĮven before PHP 7 there was an interesting feature that over time is showing more and more adoption. If the users of your classes do not want to benefit from strict typing, they just need to not add that statement in their calling scripts. So the tip for those developers that are not aware of this detail, always add declare(strict_types=1) at the top of all your class files. If strict typing is not enabled by adding declare(strict_types=1) statement at the top of each PHP class source code file, it will still not be enabled even if the calling script file also has declare(strict_types=1), which is also a required condition. Strict typing allows you to catch bugs that may cause your code to pass values of the wrong types. One curious fact about many PHP 7 only classes is that, despite they even use scalar type hinting for function arguments and return values, many classes are not enabling strict typing. Help Your Class Users Benefit of Strict Typing when They Want Therefore, some time ago it was created a new category for PHP 7 only of packages in PHP Classes. Nevertheless, we are already seeing many PHP 7 only packages being submitted to PHP Classes and other repositories.

One of the most important features is the possibility to enable strict typing, scalar type hinting for function arguments and return values.ĭespite the importance of this event and the excitement among the PHP community, many developers are still being cautious to migrate to PHP 7 because of the maintenance work that it may require to handle backwards incompatible changes that affect their applications developed in the past. Important Well Known Modern PHP Practices PHP 7 Only PackagesĪs you should know by now, the greatest event of last year was the launch of PHP 7.
