SO
Problem
This is not well known, but PHP allows you to compare objects - and not only on equality ==- but also on <and on >. But how does it work? So, if I want to create comparable objects - what restrictions / rules should they follow?
The most useful case is objects DateTime()- they have a specific timestamp, and they can be compared (and this makes logical sense). There is some explanation for lxr for DateTime. But what about the usual case?
I have:
class C
{
protected $holder;
protected $mirror;
public function __construct($h = null)
{
$this->holder=$h;
$this->mirror=-1*$h;
}
}
$one = new C(1);
$two = new C(2);
var_dump($one>$two, $one==$two, $one<$two);
-If I reorder ads he will use $mirror:
class C
{
protected $mirror;
protected $holder;
public function __construct($h = null)
{
$this->holder=$h;
$this->mirror=-1*$h;
}
}
$one = new C(1);
$two = new C(2);
var_dump($one>$two, $one==$two, $one<$two);
, , "" . protected .
:
class Test
{
protected $a;
protected $b;
function __construct($a, $b)
{
$this->a = $a;
$this->b = $b;
}
}
$x = new Test(1, 2);
$y = new Test(1, 3);
var_dump($x < $y, $x == $y, $x > $y);
$x = new Test(3, 1);
$y = new Test(2, 1);
var_dump($x < $y, $x == $y, $x > $y);
- . - . , . ,
: , ? , :
- , PHP ?
- , ? (.. )
protected/private ?
e.t.c. - , //, - , . ==/===. , - , false ().