Ошибка Uncaught Error: Call to a member function addYear() on string

Делаю задание с https://code.mu/ru/php/book/oop/class/Date/ и столкнулся с проблемой: дает ошибку на строке echo (new Date('2025-12-31'))->subDay(3)->addYear(1); "Fatal error: Uncaught Error: Call to a member function addYear() on string in ..."

<?php
    class Date
    {
        private $result;
        public function __construct($date = null)
        {
            if ($date) {
                $this->result = strtotime($date);
            }
            else {
                $this->result = time();
            }
        }

        public function getDay()
        {
            return date('d', $this->result);
        }

        public function getMonth($lang = null)
        {
            $arr = ['January' => 'Январь', 'February' => 'Февраль', 'March' => 'Март', 'April' => 'Апрель', 'May' => 'Май', 'June' => 'Июнь', 'July' => 'Июль',
                'August' => 'Август', 'September' => 'Сентябрь', 'October' => 'Октябрь', 'November' => 'Ноябрь', 'December' => 'Декабрь'];
            if ($lang == 'ru') {
                foreach ($arr as $key => $item) {
                    if ($key == date('F', $this->result)) {
                        return $item;
                    }
                }
            }
            if ($lang == 'en') {
                return date('F', $this->result);
            }
            if (empty($lang)) {
                return date('m', $this->result);
            }
        }

        public function getYear()
        {
            return date('Y', $this->result);
        }

        public function getWeekDay($lang = null)
        {
            $arr = ['Monday' => 'Понедельник', 'Tuesday' => 'Вторник', 'Wednesday' => 'Среда', 'Thursday' => 'Четверг', 'Friday' => 'Пятница', 'Saturday' => 'Суббота', 'Sunday' => 'Восресенье'];
            if ($lang == 'ru') {
                foreach ($arr as $key => $item) {
                    if ($key == date('l', $this->result)) {
                        return $item;
                    }
                }
            }
            if ($lang == 'en') {
                return date('l', $this->result);
            }
            if (empty($lang)) {
                return date('w', $this->result);
            }
        }

        public function addDay($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "$value day");
            $this->result = date_format($date, "Y-m-d");
            return $this->result;
        }

        public function subDay($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "-$value day");
            $this->result = date_format($date,"Y-m-d");
            return $this->result;
        }

        public function addMonth($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "$value month");
            $this->result = strtotime(date_format($date, "Y-m-d"));
            return $this->result;
        }

        public function subMonth($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "-$value month");
            $this->result = strtotime(date_format($date,"Y-m-d"));
            return $this->result;
        }

        public function addYear($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "$value year");
            $this->result = date_format($date, "Y-m-d");
            return $this->result;
        }

        public function subYear($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "-$value year");
            $this->result = date_format($date,"Y-m-d");
            return $this->result;
        }

        public function format($format)
        {
        return date("$format", $this->result);
        }

        public function __toString()
        {
            return (string) date("Y-m-d", $this->result);
        }
    }


$date = new Date('2025-12-31');

echo $date->getYear().'<br>';  // выведет '2025'
echo $date->getMonth().'<br>'; // выведет '12'
echo $date->getDay().'<br>';   // выведет '31'

echo $date->getWeekDay().'<br>';     // выведет '3'
echo $date->getWeekDay('ru').'<br>'; // выведет 'среда'
echo $date->getWeekDay('en').'<br>'; // выведет 'wednesday'


echo (new Date('2025-12-31'))->addYear(1).'<br>'; // '2026-12-31'
echo (new Date('2025-12-31'))->addDay(1).'<br>';  // '2026-01-01'

echo (new Date('2025-12-31'))->subDay(3)->addYear(1); // '2026-12-28'

Ошибка возникает судя по всему что я делаю return $this->result, а не просто $this в методе addYear, но если верну просто $this, то тогда показывает неправильную дату. Просьба помочь.


Ответы (1 шт):

Автор решения: ZAK ATTACK

Решение

<?php
    class Date
    {
        public $result;
        public function __construct($date = null)
        {
            if ($date) {
                $this->result = strtotime($date);
            }
            else {
                $this->result = time();
            }
        }

        public function getDay()
        {
            return date('d', $this->result);
        }

        public function getMonth($lang = null)
        {
            $arr = ['January' => 'Январь', 'February' => 'Февраль', 'March' => 'Март', 'April' => 'Апрель', 'May' => 'Май', 'June' => 'Июнь', 'July' => 'Июль',
                'August' => 'Август', 'September' => 'Сентябрь', 'October' => 'Октябрь', 'November' => 'Ноябрь', 'December' => 'Декабрь'];
            if ($lang == 'ru') {
                foreach ($arr as $key => $item) {
                    if ($key == date('F', $this->result)) {
                        return $item;
                    }
                }
            }
            if ($lang == 'en') {
                return date('F', $this->result);
            }
            if (empty($lang)) {
                return date('m', $this->result);
            }
        }

        public function getYear()
        {
            return date('Y', $this->result);
        }

        public function getWeekDay($lang = null)
        {
            $arr = ['Monday' => 'Понедельник', 'Tuesday' => 'Вторник', 'Wednesday' => 'Среда', 'Thursday' => 'Четверг', 'Friday' => 'Пятница', 'Saturday' => 'Суббота', 'Sunday' => 'Восресенье'];
            if ($lang == 'ru') {
                foreach ($arr as $key => $item) {
                    if ($key == date('l', $this->result)) {
                        return $item;
                    }
                }
            }
            if ($lang == 'en') {
                return date('l', $this->result);
            }
            if (empty($lang)) {
                return date('w', $this->result);
            }
        }

        public function addDay($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "$value day");
            $this->result =  strtotime(date_format($date, "Y-m-d"));
            return $this;
        }

        public function subDay($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "-$value day");
            $this->result =  strtotime(date_format($date, "Y-m-d"));
            return $this;
        }

        public function addMonth($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "$value month");
            $this->result = strtotime(date_format($date, "Y-m-d"));
            return $this;
        }

        public function subMonth($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "-$value month");
            $this->result = strtotime(date_format($date,"Y-m-d"));
            return $this;
        }

        public function addYear($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "$value year");
            $this->result =  strtotime(date_format($date, "Y-m-d"));
            return $this;
        }

        public function subYear($value)
        {
            $date = date_create(date("Y-m-d", $this->result));
            date_modify($date, "-$value year");
            $this->result =  strtotime(date_format($date, "Y-m-d"));
            return $this;
        }

        public function format($format)
        {
        return date("$format", $this->result);
        }

        public function __toString()
        {
            return (string) date("Y-m-d", $this->result);
        }
    }

$date = new Date('2025-12-31');

echo $date->getYear().'<br>';  // выведет '2025'
echo $date->getMonth().'<br>'; // выведет '12'
echo $date->getDay().'<br>';   // выведет '31'

echo $date->getWeekDay().'<br>';     // выведет '3'
echo $date->getWeekDay('ru').'<br>'; // выведет 'среда'
echo $date->getWeekDay('en').'<br>'; // выведет 'wednesday'


echo (new Date('2025-12-31'))->addYear(1).'<br>'; // '2026-12-31'
echo (new Date('2025-12-31'))->addDay(1).'<br>';  // '2026-01-01'

echo (new Date('2025-12-31'))->subDay(3)->addYear(1).'<br>'; // '2026-12-28'


?>
→ Ссылка