Semantics of adding an Object and an integer to each other, in PHP? -
class wat { public $a = 3.14; public $x = 9; public $y = 2; } $a = new wat(); var_dump(1000 + $a); var_dump($a + 1000);
the output is:
int(1001) int(1001)
well, adding wat* object integer not right thing do, since php complains "object of class wat not converted int", still, do?
(i have practical reason asking this, want refactor function rid of "php notice", while still keeping behaviour unchanged.)
*: http://img.youtube.com/vi/kxegk1hdze0/1.jpg
addition (+
) implicitly casts both operands float
if either 1 of them float
, otherwise both operands cast int
(see paragraph #2.)
it seems that, @ least now, object
cast int
results in value 1
, hence result 1000 + 1 = 1001
or 1 + 1000 = 1001
, however, per documentation, behavior undefined , should not relied upon.
if you've turned on e_notice
error reporting, notice should produced, saying object not converted int.
Comments
Post a Comment