PHP Что делать не работает код?
не пойму в чём проблема. Код должен быть рабочий но вместо ожидаемого результата ошибки. PHP 7.4 стоит.
Fatal error: Uncaught TypeError: Typed property BarsAPI\Lesson::$homework must be string or null, array used in /www/wwwroot/diary/php/Diary/Lesson.php:32 Stack trace: #0 /www/wwwroot/diary/php/Diary/Day.php(22): BarsAPI\Lesson->__construct() #1 [internal function]: BarsAPI\Day->BarsAPI\{closure}() #2 /www/wwwroot/diary/php/Diary/Day.php(22): array_map() #3 /www/wwwroot/diary/php/Diary/Diary.php(20): BarsAPI\Day->__construct() #4 [internal function]: BarsAPI\Diary->BarsAPI\{closure}() #5 /www/wwwroot/diary/php/Diary/Diary.php(20): array_map() #6 /www/wwwroot/diary/php/User.php(71): BarsAPI\Diary->__construct() #7 /www/wwwroot/diary/index.php(18): BarsAPI\User->getDiary() #8 {main} thrown in /www/wwwroot/diary/php/Diary/Lesson.php on line 32
$diary = $api->user->getDiary ();
echo $diary;
Lesson.php:
<?php
namespace BarsAPI;
/**
* Объект представления урока
*/
class Lesson
{
public ?string $date = null; // Дата проведения урока
public ?string $discipline = null; // Предмет
public ?string $teacher = null; // Учитель
public ?string $subject = null; // Тема
public ?string $room = null; // Кабинет
public ?string $homework = null; // Домашняя работа
public array $marks = []; // Оценки
public ?int $lesson_id = null; // ID урока
public ?string $lesson_name = null; // Имя урока (1-й урок, 2-й урок и т.п.)
public ?string $begin_time = null; // Время начала (8:15 и т.п.)
public ?string $end_time = null; // Время конца (8:55 и т.п.)
/**
* Конструктор объекта
*
* @param array $info - массив информации о уроке
*/
public function __construct (array $info)
{
foreach (get_object_vars ($this) as $name => $value)
if (isset ($info[$name]))
$this->$name = $info[$name];
$this->lesson_id = $info['lesson'][0] ?? null;
$this->lesson_name = $info['lesson'][1] ?? null;
$this->begin_time = $info['lesson'][2] ?? null;
$this->end_time = $info['lesson'][3] ?? null;
foreach ($this->marks as $id => $mark)
$this->marks[$id] = new Mark (current ($mark[2]), $mark[0]);
}
/**
* Проход по оценкам
*
* @param callable $callable - коллбэк для вызова
*
* @return Lesson
*/
public function foreach (callable $callable): Lesson
{
foreach ($this->marks as $mark)
$callable ($mark);
return $this;
}
}
Day.php:
<?php
namespace BarsAPI;
/**
* Объект представления дня недели
*/
class Day
{
public ?string $date = null; // Дата
public array $lessons = []; // Список уроков
/**
* Конструктор объекта
*
* @param array $info - массив информации о дне недели
*/
public function __construct (array $info)
{
$this->date = $info[0] ?? null;
$this->lessons = array_map (fn ($lesson) => new Lesson ($lesson), $info[1]['lessons'] ?? []);
}
/**
* Проход по урокам
*
* @param callable $callable - коллбэк для вызова
*
* @return Day
*/
public function foreach (callable $callable): Day
{
foreach ($this->lessons as $lesson)
$callable ($lesson);
return $this;
}
}
Diary.php:
<?php
namespace BarsAPI;
/**
* Объект представления журнала
*/
class Diary
{
public array $days = []; // Список дней
/**
* Конструктор объекта
* В качестве параметра нужно передавать информацию, получаемую от метода REST API "rest/diary"
*
* @param array $info - массив информации о журнале
*/
public function __construct (array $info)
{
$this->days = array_map (fn ($day) => new Day ($day), $info['days'] ?? []);
}
/**
* Проход по дням недели
*
* @param callable $callable - коллбэк для вызова
*
* @return Diary
*/
public function foreach (callable $callable): Diary
{
foreach ($this->days as $day)
$callable ($day);
return $this;
}
}
User.php:
<?php
namespace BarsAPI;
/**
* Объект предоставления информации о пользователе
*/
class User
{
protected Bars $bars; // Владелец данного объекта
public ?string $name = null; // ФИО пользователя
public ?string $public_name = null;
public ?string $school = null;
public ?int $pupil_id = null; // ID ученика
public ?int $profile_id = null; // ID профиля
public ?int $id = null;
public ?array $childs = null;
public function __construct (Bars $owner, array $info)
{
$this->bars = $owner;
$this->name = $info['fio'] ?? null;
$this->public_name = $info['childs'][0][1] ?? null;
$this->school = $info['childs'][0][2] ?? null;
$this->pupil_id = $info['childs'][0][0] ?? null;
$this->profile_id = $info['profile_id'] ?? null;
$this->id = $info['id'] ?? null;
$this->childs = $info['childs'] ?? null;
}
public function getDiary (int $to_date = null, int $from_date = null): Diary
{
$to_date ??= time ();
$from_date ??= $to_date;
$diary = $this->bars->request ('diary', [
'pupil_id' => $this->pupil_id,
'from_date' => date ('d.m.Y', $from_date),
'to_date' => date ('d.m.Y', $to_date)
]);
if ($diary === null || !@$diary['success'])
throw new \Exception ('An exception catched when trying to get diary');
return new Diary ($diary);
}
}