1 | <?php |
||
23 | class TypeAhead extends InputWidget |
||
24 | { |
||
25 | /** |
||
26 | * @var array the options for the Bootstrap TypeAhead JS plugin. |
||
27 | * Please refer to the Bootstrap TypeAhead plugin Web page for possible options. |
||
28 | * @see https://github.com/twitter/typeahead.js#usage |
||
29 | */ |
||
30 | public $clientOptions = []; |
||
31 | /** |
||
32 | * @var array the event handlers for the Bootstrap TypeAhead JS plugin. |
||
33 | * Please refer to the Bootstrap TypeAhead plugin Web page for possible events. |
||
34 | * @see https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#custom-events |
||
35 | */ |
||
36 | public $clientEvents = []; |
||
37 | /** |
||
38 | * @var array the datasets object arrays of the Bootstrap TypeAhead Js plugin. |
||
39 | * @see https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#datasets |
||
40 | */ |
||
41 | public $dataSets = []; |
||
42 | /** |
||
43 | * @var array of [[Bloodhound]] instances. Please note, that the widget is just calling the object to return its |
||
44 | * client script. In order to use its adapter, you will have to set it on the widget [[dataSets]] source option |
||
45 | * and using the object instance as [[Bloodhound::getAdapter()]] method. This is required to be able to use multiple |
||
46 | * datasets with bloodhound engine. |
||
47 | * @see https://gist.github.com/jharding/9458772#file-remote-js |
||
48 | */ |
||
49 | public $engines = []; |
||
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | 3 | public function run() |
|
63 | |||
64 | /** |
||
65 | * Registers Twitter TypeAhead Bootstrap plugin and the related events |
||
66 | */ |
||
67 | 3 | protected function registerClientScript() |
|
104 | } |
||
105 |
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.