1 | <?php |
||
25 | class AssignmentsWidget extends Widget |
||
26 | { |
||
27 | use AuthManagerAwareTrait; |
||
28 | use ContainerAwareTrait; |
||
29 | |||
30 | /** |
||
31 | * @var int ID of the user to whom auth items will be assigned |
||
32 | */ |
||
33 | public $userId; |
||
34 | /** |
||
35 | * @var string[] the post parameters |
||
36 | */ |
||
37 | public $params = []; |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | * |
||
42 | * @throws InvalidConfigException |
||
43 | */ |
||
44 | public function init() |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | * |
||
55 | * @throws InvalidParamException |
||
56 | * @throws InvalidConfigException |
||
57 | */ |
||
58 | public function run() |
||
79 | |||
80 | /** |
||
81 | * Returns available auth items to be attached to the user. |
||
82 | * |
||
83 | * @param int|null type of auth items or null to return all |
||
84 | * @param null|mixed $type |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | protected function getAvailableItems($type = null) |
||
100 | } |
||
101 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.