The problem is that you are trying to create an array with a non-literal value It would probably be better to use a class member instead of trying to create a function-dependent value within the array
.
How did you not explain exactly how you will use the $arr
, I don’t know if this is good enough, but here’s a small example of how to initialize a given in the function constructor:
class MinhaClasse
{
public $arr = array();
function __construct() {
$this->arr['ano'] = date('Y');
}
public function getAno()
{
return $this->arr['ano'];
}
}
However, this generates a side effect, which can be seen in IDEONE:
http://ideone.com/gnqcuC
Since the value can change with each instantiation of the object, it is probably not what you want. It would be nice to specify at what point in fact you will need the value, because maybe it is the case of static
, or pass the value in the constructor. If you will use static
, needs a if( isset( ) )
in the constructor, if you don’t want to change the value of all instances and keep the first occurrence only (I think it leaves the code a bit "unsafe" inclusive, unless you have a deep domain of what you’re doing).
The var
, in PHP 5 it is synonymous with public
, but gives a Warning (or error, if you work with STRICT mode).
Maybe the solution is not to put the year in the creation of the class, but to define in PHP when using it even:
class MinhaClasse
{
public $arr = array();
public function setAno( $ano ) {
$this->arr['ano'] = $ano;
}
public function getAno()
{
return $this->arr['ano'];
}
}
And when it’s time to use:
$objeto = new MinhaClasse();
$objeto->setAno( date( 'Y' ) );
// agora vc usa o objeto onde precisar
Take out the word
var
because it is PHP 4. Lavarel works only with PHP 5+– StillBuggin