Question from the PHP8 test

How to use the `__construct()` method in PHP.

Medium

What does this example show?

<?php

class Period {
    private \DateTimeInterface|null $startDate;

    public function __construct(?\DateTimeInterface $startAt)
    {
            $this->startDate = $startAt;
    }

    public function getStartDate(): \DateTimeInterface
    {
       return $this->startDate;
    }
}

$period = new Period(new \DateTimeImmutable("2020-11-26"));
$period->__construct();

$dateAsString = $period->getStartDate()?->format('Y');

echo $dateAsString;
Author: Amine BouchamStatus: Published(Update)Question passed 123 times
Edit
2
Community Evaluations
developer avatar
Vincent Cotro
05/09/2022
Ahah, merci pour la correction et bien vu 😂