I would like to insert the current year into an array property of a class.
I tried to use the date('Y')
function directly in the property assignment, but it did not work.
<?php
class MyClass {
public $myArray = [
'teste' => 'valor',
'ano' => date('Y')
];
// código
}
var_dump( (new MyClass())->myArray );
Temporarily, I solved this problem by initializing this value in the constructor method of the class:
<?php
class MyClass {
public $myArray = [
'teste' => 'valor',
'ano' => ''
];
public function __construct()
{
$this->myArray['ano'] = date('Y');
}
// código
}
var_dump( (new MyClass())->myArray );
Is there any way to assign the result of a function to a property, without resorting to an assignment through the constructor?
I would not like to use the constructor because I plan to leave this variable as static