shitbangs•

PHP uses quantum memory for its variables, observing them changes their value. That's ok because quantom memory has web scale performance.

7
refuse_human•

Those calls to var_dump() automatically run the __toString() method before evaluating the comparisons.

Without looking at the DateInterval class definition, I'd wager there is some aspect of the DateInterval class which affects its internal state (and/or the value returned by its __toString() method) when the $i1 variable is dumped.

Try dumping (string)$i1 and (string)$i2 to see what's being returned for comparison before and after dumping $i1.

6
billybolero•

Interesting. Sure seems odd to modify state in a toString method though.

5
RitchieRamirez•

A lot of things in PHP could be described as "odd".

16
allthediamonds•

Okay, that's "why" as in "what chain of events has this behaviour as its consequence", but I'm still really interested about "why" as in "what is the reasoning behind making it work this way". In other words, is this intended behaviour, or a bug?

1
refuse_human•

In other words, is this intended behaviour, or a bug?

Can't comment on the intentions of anyone else's code, though I hear good intentions make great pavement.

FWIW, you could roll your own interval management class with strtotime() to do exactly what you'd like to do here*.

* Interestingly enough, strtotime()'s behavior changed in PHP 5.3 ...

1
callcifer•

Are you using PHP 5.2 by any chance? DateTime was notoriously broken pre-5.3 and IIRC this was one of those bugs.

4
notian•

Just tested on 5.4.1 and it's the same.

Also, it appears that the __wakeup method is where the change occurs:

$y = new DateInterval('P1Y');
$x = new DateInterval('P2Y');
echo ($y > $x) ? "t" : "f"; //f
$y->__wakeup();
echo ($y > $x) ? "t" : "f"; // t

but strangely, if you $x->__wakeup(), it goes back to 'f'.

But the root of the problem is that DateInterval hasn't implemented any kind of comparison.

2
felds•

FTFY:

class SchroedingerDateInterval extends \DateInterval {}
2
evil_guy•

Different behavior on HHVM

1
drgomesp•

Apparently, there is a bug that makes it difficult to compare two DateInterval objects in PHP like you can do with DateTime objects. In any case, the var_dump is, in fact, interfering in the state of the object and this is indeed a weird thing.

1