# Scalar Type Casting Magic Methods

- Thread: https://externals.io/message/67304
- Original archive: https://news-web.php.net/php.internals/67304
- Started by Oleku Konko on 2013-05-04T15:31:13+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #67304 — Oleku Konko — 2013-05-04T15:31:13+00:00

URL: https://externals.io/message/67304#67304

Quick Observations : 

I had this issue ( http://stackoverflow.com/questions/16375331/increment-on-tostring ) and it kept me thinking why no error was returned, then i stumble up this RFC :  https://wiki.php.net/rfc/object_cast_to_types

The patch is basic , simple and working and it should be seriously considered in 5.5.  I want to  believe if https://github.com/php/php-src/pull/334 can make it to PHP 5.5 then we should give object_cast_to_types the same opportunity except they are string objections why it should not be so


Thanks
Oleku Konko

---

## #67306 — Nikita Popov — 2013-05-05T07:55:48+00:00

URL: https://externals.io/message/67304#67306 · In reply to: #67304

> Quick Observations :
>
> I had this issue (
> http://stackoverflow.com/questions/16375331/increment-on-tostring ) and
> it kept me thinking why no error was returned, then i stumble up this RFC :
>  https://wiki.php.net/rfc/object_cast_to_types
>
> The patch is basic , simple and working and it should
> be seriously considered in 5.5.  I want to  believe if
> https://github.com/php/php-src/pull/334 can make it to PHP 5.5 then we
> should give object_cast_to_types the same opportunity except they are
> string objections why it should not be so
>

This can't go into PHP 5.5 anymore as we're already past feature freeze.
Nikita

---

## #67307 — Oleku Konko — 2013-05-05T08:03:21+00:00

URL: https://externals.io/message/67304#67307 · In reply to: #67306

Thanks for the reply. I hope it would be part of next release eg 5.5.1
Oleku


________________________________
 From: Nikita Popov <nikita.ppv@gmail.com>
To: Oleku Konko <oleku.konko@yahoo.com> 
Cc: "php-internals@lists.php.net" <php-internals@lists.php.net>; "internals@lists.php.net" <internals@lists.php.net> 
Sent: Sunday, May 5, 2013 8:55 AM
Subject: Re: [PHP-DEV] Scalar Type Casting Magic Methods
 




Quick Observations : 
>
>I had this issue ( http://stackoverflow.com/questions/16375331/increment-on-tostring ) and it kept me thinking why no error was returned, then i stumble up this RFC :  https://wiki.php.net/rfc/object_cast_to_types
>
>The patch is basic , simple and working and it should be seriously considered in 5.5.  I want to  believe if https://github.com/php/php-src/pull/334 can make it to PHP 5.5 then we should give object_cast_to_types the same opportunity except they are string objections why it should not be so
>
 This can't go into PHP 5.5 anymore as we're already past feature freeze.
Nikita

---

## #67308 — Pierre Joye — 2013-05-05T08:15:01+00:00

URL: https://externals.io/message/67304#67308 · In reply to: #67307

hi!


>
>
> Thanks for the reply. I hope it would be part of next release eg 5.5.1

More 5.6. Features freeze for 5.5.x.

Cheers,
Pierre

---

## #67345 — Nikita Popov — 2013-05-07T18:37:40+00:00

URL: https://externals.io/message/67304#67345 · In reply to: #67304

> Quick Observations :
>
> I had this issue (
> http://stackoverflow.com/questions/16375331/increment-on-tostring ) and
> it kept me thinking why no error was returned, then i stumble up this RFC :
>  https://wiki.php.net/rfc/object_cast_to_types
>
> The patch is basic , simple and working and it should
> be seriously considered in 5.5.  I want to  believe if
> https://github.com/php/php-src/pull/334 can make it to PHP 5.5 then we
> should give object_cast_to_types the same opportunity except they are
> string objections why it should not be so
>

I don't think that this would be particularly useful and as such I don't
think we need it. The idea of doing some meaningful operation when writing
$obj + 1 is nice, but only if you have actual operator overloading and can
compute the result of that expression yourself. If you don't have this
possibility and only get to cast $a to an integer/float, then the whole
thing becomes rather useless. The only thing it could be used for are thin
wrappers around integers/floats and I don't see why one would want to do
such a thing (especially as you get back a number and *not* a wrapper
object).

Thus, __toScalar(), __toInt(), __toFloat() seem pretty useless. The only
thing that might have some merit is __toArray(). But there again, I'm not
really sure what it gives us. After all we already have Traversable and
ArrayAccess, so we can already make an object behave pretty much like an
array.

Nikita

---

## #67369 — Pierre du Plessis — 2013-05-08T05:37:59+00:00

URL: https://externals.io/message/67304#67369 · In reply to: #67345

The __toArray can be useful if you want to perform array functions on the
object (E.G array_filter), otherwise I think it would be very useful if the
array functions can accept an object implementing ArrayAccess as well
instead of just an array


> 
>
> > […]
> RFC :
> > […]
>
> I don't think that this would be particularly useful and as such I don't
> think we need it. The idea of doing some meaningful operation when writing
> $obj + 1 is nice, but only if you have actual operator overloading and can
> compute the result of that expression yourself. If you don't have this
> possibility and only get to cast $a to an integer/float, then the whole
> thing becomes rather useless. The only thing it could be used for are thin
> wrappers around integers/floats and I don't see why one would want to do
> such a thing (especially as you get back a number and *not* a wrapper
> object).
>
> Thus, __toScalar(), __toInt(), __toFloat() seem pretty useless. The only
> thing that might have some merit is __toArray(). But there again, I'm not
> really sure what it gives us. After all we already have Traversable and
> ArrayAccess, so we can already make an object behave pretty much like an
> array.
>
> Nikita

---

## #67371 — Ferenc Kovacs — 2013-05-08T08:42:58+00:00

URL: https://externals.io/message/67304#67371 · In reply to: #67369

2013.05.08. 7:38, "Pierre du Plessis" <pierre@pcservice.co.za> ezt írta:
>
> The __toArray can be useful if you want to perform array functions on the
> object (E.G array_filter), otherwise I think it would be very useful if
the
> array functions can accept an object implementing ArrayAccess as well
> instead of just an array

yeah, I also think that ArrayObjects should be accepted where array is
expected.

---

## #67415 — johannes@schlueters.de — 2013-05-13T08:18:25+00:00

URL: https://externals.io/message/67304#67415 · In reply to: #67371

> 2013.05.08. 7:38, "Pierre du Plessis" <pierre@pcservice.co.za> ezt írta:
> > […]

This can't work.
Consider 
   <?php
   class C {
       public function __toString() { return "string"; }
       public function __toArray() { return []; }
   }
   $result = str_replace(new C, new C, new C);
   ?>
Which method will be called?

> > […]
> 
> yeah, I also think that ArrayObjects should be accepted where array is
> expected.

Patches are certainly welcome, but be sure to get to every place, else
it becomes an inconsistent mess :)

Oh, and be consistent about return values of operations ... oh, and will
   $result = some_array_operation(new ArrayObject);
return an array or instance of ArrayObject, if the later how will it be
instantiated ... do we have to pass result factories or something to all
of those?

johannes
