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:


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.)



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

Writing email addresses to a file from a file using regex in perl

regex perl

Ive got a file containing many email addresses, ip address and dates. I need to write a perl script to read in the file, use a regex expression to detect each of these and output them to an other f...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.