2
I would like to insert the current year into an array type property of a class.
I tried to use the function date('Y')
directly in the attribution of the property, however as 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 class constructor method:
<?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 via the constructor?
I would not like to use the constructor because I plan to leave this variable as static
As far as I know, it is not possible to define a property with function value.
– Papa Charlie