Почему я получаю Undefined variable?
Есть такой код класса
class genMailData {
public $mailData = [];
public function set_xrand() {
$this->mailData['test'] = rand(1234, 99999999);
}
public function result() {
return $this->$mailData;
}
}
Вызываю так:
$GMD = new genMailData();
$GMD->set_xrand();
var_export( $GMD->result() );
В ответ получаю
Notice: Undefined variable: mailData in test.php on line 15
Notice: Undefined property: genMailData::$ in test.php on line 15
NULL
Ругается на эту строку:
public function result() {
return $this->$mailData; //на эту строку ругается
}
php 7.3
Ответы (1 шт):
Автор решения: Алексей Шиманский
→ Ссылка
Потому что не
$this->$mailData;
а
$this->mailData;
Советую поучить основы ООП получше