1 | <?php |
||
26 | final class AlertComponentTwigExtension extends AbstractComponentTwigExtension { |
||
27 | |||
28 | /** |
||
29 | * Service name. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.component.alert"; |
||
34 | |||
35 | /** |
||
36 | * Constructor. |
||
37 | */ |
||
38 | public function __construct() { |
||
41 | |||
42 | /** |
||
43 | * Displays a Bootstrap alert "Danger". |
||
44 | * |
||
45 | * @param array $args The arguments. |
||
46 | * @return string Returns the Bootstrap alert "Danger". |
||
47 | */ |
||
48 | public function bootstrapAlertDangerFunction(array $args = []) { |
||
51 | |||
52 | /** |
||
53 | * Displays a Bootstrap alert "Info". |
||
54 | * |
||
55 | * @param array $args The arguments. |
||
56 | * @return string Returns the Bootstrap alert "Info". |
||
57 | */ |
||
58 | public function bootstrapAlertInfoFunction(array $args = []) { |
||
61 | |||
62 | /** |
||
63 | * Displays a Bootstrap alert "Success". |
||
64 | * |
||
65 | * @param array $args The arguments. |
||
66 | * @return string Returns the Bootstrap alert "Success". |
||
67 | */ |
||
68 | public function bootstrapAlertSuccessFunction(array $args = []) { |
||
71 | |||
72 | /** |
||
73 | * Displays a Bootstrap alert "Warning". |
||
74 | * |
||
75 | * @param array $args The arguments. |
||
76 | * @return string Returns the Bootstrap alert "Warning". |
||
77 | */ |
||
78 | public function bootstrapAlertWarningFunction(array $args = []) { |
||
81 | |||
82 | /** |
||
83 | * Get the Twig functions. |
||
84 | * |
||
85 | * @return Twig_SimpleFunction[] Returns the Twig functions. |
||
86 | */ |
||
87 | public function getFunctions() { |
||
96 | |||
97 | /** |
||
98 | * Displays a Bootstrap link alert. |
||
99 | * |
||
100 | * @param array $args The arguments. |
||
101 | * @return string Returns the Bootstrap link alert. |
||
102 | */ |
||
103 | public function bootstrapLinkAlertFunction(array $args = []) { |
||
117 | |||
118 | } |
||
119 |
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.