I do agree that, if I'm the owner and responsible for the object in
question, adding an explicit method would be better than casting it. The
problem is that many times we are dealing with third party libraries, and
modifying them is more complicated. But it is true, and it is true that the
number types would collide if both could be used. That could issue an
E_NOTICE if not force casted, and default to one of the types (as unquoted
strings do).

El mié., 13 de may. de 2015 a la(s) 8:41 p. m., Christoph Becker <
cmbecke...@gmx.de> escribió:

> Guido Contreras Woda wrote:
>
> > So, what do you think of a set of interfaces that allow userland objects
> to
> > be used as scalar types?
> >
> > Some simple examples in PHP here:
> > https://gist.github.com/guiwoda/5d16c8fb97d29e476d20
>
> 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.
>
> 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()
>
> where the latter is a normal user defined method.
>
> Another problem is that such automatic casting from objects to scalars
> might obscure the code.
>
> --
> Christoph M. Becker
>
>
>

Reply via email to