GitHub

CI4-Strict.tk

Ver: 4.0.0-rc.3 - Build: 2c04e1c (previous: a424b3e)

Declare Strict_types - Myths and Clarification

 

TL;DR;
Instead study the following examples...   try to guess the result before selecting a Test(...) option:
Test Strict_types
 

Coding with Strict_types=1
is like
driving with a seatbelt...
and an airbag!


¯\_(ツ)_/¯


PHP - Brief History

Introduced nearly 25 years ago! Originally designed to simply enhance web development. The simplistic approach necessitated a large amount of script to make writing HTML script easy.

Unfortunately making PHP more user friendly increased processing time and PHP aquired a reputation for being very slow...

 
PHP 7 has overcome slow processing...

Released December 2015, it not only but also achieved better performance but also does not affect legacy code!

Most performance is due to eliminating "Type juggling".

Native PHP functions now have all function parameters types and return types set!

This feature would have made all legacy libraries fail but has been cunningly implemented and applies **ONLY** if the following declaration is the first file statement.

<?php declare(strict_types=1);

The strict_types validation checks only apply to the file where the declaration has been set!

This new feature is vastly misunderstood and can be simplified with the following three usage types which cover every eventuality:

  1. Do not use the feature - ideal for **OLD** external libraries.
  2. Declare strict_types - usage:
    • Native PHP function parameter types must match otherwise errors will be activated.
    • Included files having functions have two options
 
 
Source
 
Why treat warnings as errors? They are just warnings! A compiler warning signals a potentially serious problem in your code. The problems listed above are almost always fatal; others may or may not be, but you want compilation to fail even if it turns out to be a false alarm. Investigate each warning, find the root cause, and fix it. In the case of a false alarm, work around it — that is, use a different language feature or construct so that the warning is no longer triggered. If this proves to be very hard, disable that particular warning on a case by case basis.
 
You don't want to just leave warnings as warnings even if all of them are false alarms. It could be OK for very small projects where the total number of warnings emitted is less than 7. Anything more, and it's easy for a new warning to get lost in a flood of old familiar ones. Don't allow that. Just cause all your project to compile cleanly.
 
Note this applies to program development. If you are releasing your project to the world in the source form, then it might be a good idea not to supply -Werror or equivalent in your released build script. People might try to build your project with a different version of the compiler, or with a different compiler altogether, which may have a different set of warnings enabled. You may want their build to succeed. It is still a good idea to keep the warnings enabled, so that people who see warning messages could send you bug reports or patches.