Re: Proposal: interfaces for object to scalar type casting
| From: | Christoph Becker | Date: | Wed, 13 May 2015 23:41:45 +0000 |
| Subject: | Re: Proposal: interfaces for object to scalar type casting | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-86203@lists.php.net to get a copy of this message | ||
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