First of all, I apologize that this question is so vague. I don’t remember what it is called, or how they work, so it’s very difficult to start a search or formulate a good headline.
I have two questions enclosed in one:
Firstly:
How do objects internally convert to other types? What is called?
Example:
$Obj{ $value = 1; $other = 2; $more = 3; } $myObj = (string)$Obj; print $myObj; // prints "1, 2, 3" or something like that
Secondly:
Can this method be used in mathematics? Is there an override function that recognizes when an object is used in math?
Example:
$Obj{ $value = 1; $other = 2; $more = 3; } $result = 4 / $Obj; print $result; // prints ".66666667" or something similar (sum of all properties)
Update:
I think this might have something to do with serialize () , but I know that I heard about the case when this is done “automatically” without calling serialize() and so that it serializes the whole object, it just converts its useful value, like my examples above.
The final:
Thanks for @trey for being right about casting and @webbiedave to point out the magic of the __ toString method.
source share