I am reading some code that looks like that:
Class_Menu.php:
<?php
class Menu {
private $label;
private $link;
private $args;
function __construct($label, $link, $args=array()) {
$this->label=$label;
$this->link=$link;
$this->args=$args;
}
...
Note how $args is defined as an array in the __construct. Why would one choose this approach rather than define $args in the var list:
<?php
class Menu {
private $label;
private $link;
private $args=array();
function __construct($label, $link, $args) {
$this->label=$label;
$this->link=$link;
$this->args=$args;
}
...
In the first example,
$argsis optional. The constructor can be called with only 2 arguments for$labeland$link, and$argswill be defined as an empty array.In the second example, the
$argsparameter is required.(opinion): I would have left
$args = array()in the constructor AND when declaring the property so it’s easy to tell at a glance what type of var $args is.