1 | <?php |
||
21 | class Gallery extends Widget |
||
22 | { |
||
23 | /** |
||
24 | * @var array the HTML attributes for the links container tag. |
||
25 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
26 | */ |
||
27 | public $options = []; |
||
28 | /** |
||
29 | * @var array the HTML attributes for the lightbox container tag. |
||
30 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
31 | */ |
||
32 | public $templateOptions = []; |
||
33 | /** |
||
34 | * @var array the options for the BlueImp Gallery plugin. |
||
35 | * Please refer to the BlueImp Gallery plugin Web page for possible options. |
||
36 | * @see https://github.com/blueimp/Gallery/blob/master/README.md#setup |
||
37 | */ |
||
38 | public $clientOptions = []; |
||
39 | /** |
||
40 | * @var array the event handlers for the underlying Bootstrap Switch 3 input JS plugin. |
||
41 | * Please refer to the [BlueImp Gallery plugin](https://github.com/blueimp/Gallery/blob/master/README.md#event-callbacks) |
||
42 | * for information about their callbacks. |
||
43 | */ |
||
44 | public $clientEvents = []; |
||
45 | /** |
||
46 | * @var array The array of items that compound the gallery. The syntax is as follows: |
||
47 | * |
||
48 | * - src: string, the image to display |
||
49 | * - url: string, the image to display on the lightbox. If none found, will display `src` |
||
50 | * - options: HTML attributes of the link |
||
51 | * - imageOptions: HTML attributes of the image to be displayed |
||
52 | */ |
||
53 | public $items = array(); |
||
54 | /** |
||
55 | * @var bool whether to display the controls on initialization |
||
56 | */ |
||
57 | public $showControls = true; |
||
58 | |||
59 | /** |
||
60 | * @inheritdoc |
||
61 | */ |
||
62 | 6 | public function init() |
|
80 | |||
81 | /** |
||
82 | * @inheritdoc |
||
83 | */ |
||
84 | 6 | public function run() |
|
93 | |||
94 | /** |
||
95 | * @return string the items that are need to be rendered. |
||
96 | */ |
||
97 | 6 | public function renderItems() |
|
105 | |||
106 | /** |
||
107 | * @param mixed $item |
||
108 | * @return null|string the item to render |
||
109 | */ |
||
110 | 6 | public function renderItem($item) |
|
126 | |||
127 | /** |
||
128 | * Renders the template to display the images on a lightbox |
||
129 | * @return string the template |
||
130 | */ |
||
131 | 3 | public function renderTemplate() |
|
143 | |||
144 | /** |
||
145 | * Registers the client script required for the plugin |
||
146 | */ |
||
147 | 3 | public function registerClientScript() |
|
166 | } |
||
167 |
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.