Hi!

> One of the biggest problems is that it's not always clear which
> conversion should be used; consider, for instance,
> 
>   $obj1 + $obj2
> 
> where __toInt() as well as __toFloat() might make sense.

If both are object, I would rather not use either conversion, I'd use
operator support instead, if operator is not supported by the objects
(or the objects do not have same operator handler) then all bets are
off. I guess we could keep them converted to int as they are now, but
it'd be something of "we must return something so we choose arbitrarily
and hope it's what you meant" than something with a robust semantics.
Primarily because for arbitrary objects there's really no semantics that
is natural here.

> A possible solution would be to force the conversion with type casts,
> but I don't see a real advantage in using
> 
>   (int) $obj1
> 
> over
> 
>   $obj1->toInt()

One advantage is that the former also accepts $obj1 being actual int.
Whether it's useful or not depends on the surrounding code of course.

> Another problem is that such automatic casting from objects to scalars
> might obscure the code.

True. I think this is one of the main reasons why it wasn't implemented.
-- 
Stas Malyshev
smalys...@gmail.com

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to