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 the wat* object to an integer is obviously not the right thing to do, since PHP complains about it with "Object of class wat could not be converted to int", but still, what does it do?

(I also have a practical reason for asking this, I want to refactor a function to get rid of the "PHP Notice", while still keeping behaviour completely unchanged.)


*:

Answers


Addition (+) implicitly casts both operands to float if either one of them is float, otherwise both operands are cast to int (See paragraph #2.)

It seems that, at least for now, an object cast to int results in the value 1, hence the result 1000 + 1 = 1001 or 1 + 1000 = 1001, however, per the documentation, the behavior is undefined and should not be relied upon.

If you've turned on E_NOTICE error reporting, a notice should also be produced, saying that object could not be converted to int.


Need Your Help

Getting into newer OpenGL

java opengl graphics

some years ago I played a lot with OpenGL2.1 writing many demos that used basic features (mainly immediate mode) without caring about the whole shader approach.

How come I can't bind this to JQuery?

javascript jquery html

$("input").bind("keyup",function(e1){