Open ElusiveMind opened 2 years ago
The only reason for which initializing a variable before using it is considered a best practice is that code is expected not to throw warnings. That's also the reason why literal strings are written in delimiters, even if PHP would interpret constant_that_does_not_exist
as 'constant_that_does_not_exist'
, when no constant_that_does_not_exist
constant is found.
PHP doesn't throw warnings when $blocks['syndicate']
is set without first initializing $blocks
to an empty array. The only reason to initialize a variable to an empty array is to avoid issues when that variable has been already used ($a = 1; $a[2] = 100;
would throw an exception in PHP 8 and a warning in other PHP versions.)
When I started writing PHP code, I was said to always initialize an array before adding content to it. Then, that suggestion was stopped to be done.
This is intriguing. I've been doing this in the same way I've been checking variables for !empty
or isset
- I considered them to be in the same vein of best practice. I suppose the first 5 years of having it beaten into my head to do it persisted for more years than I care to admit.
Also happy to not have to do it. :)
Description of the bug
In the documentation for hook_block_info, the $blocks array is not initialized before defining elements within the block. In the interest of best practices, should this be initialized prior to defining the elements in the array?
Steps To Reproduce
Go to: https://docs.backdropcms.org/api/backdrop/core%21modules%21layout%21layout.api.php/function/hook_block_info/1
Actual behavior
There is no init of the $blocks variable
Expected behavior
$blocks = array();
should appear after the function definition.
Additional information
I welcome discussion on this. While I realize this may or may not cause warnings in PHP, it is considered a best practice.