How to implement object comparison in php?

As you can do for your class can compare objects as, for example, this is implemented in the DateTime class? Let's say I have an event class that has a field with date:

<?php
class Event {
 public $time;
 public $title;

 public function __construct($time, $title) {
 $this--->time = $time;
 $this->title = $title;
}
}

$ev1 = new Event(new DateTime('2017-10-02'), 'first event');
$ev2 = new Event(new DateTime('2017-10-05'), 'second event');

I would like to compare them thus:
if ($ev1 < $ev2) {
 echo $ev1->title . 'is earlier';
}

instead of the more cumbersome:
if ($ev1->time < $ev2->time) {
 echo $ev1->title . 'is earlier';
}
June 14th 19 at 20:44
1 answer
June 14th 19 at 20:46

Find more questions by tags PHP