I like to program object oriented so I want to make my own IntegerObject class. But when I try to execute the following code:
$x = new IntegerObject(3)
echo $x / 3;
I get the error:
Object of class IntegerObject could not be converted to int
Is there a magic function like __toString() to cast an object to an integer?
Workaround
As PHP is dynamically type casting language you can magically cast to string and the PHP will cast it to integer:
$x = new IntegerObject(3);
echo "$x" / 3;
You can cast to an integer using the (int)
operator:
$x = new IntegerObject(3);
var_dump((int) "$x" / 3);
That should show you that the result is an int.