# Add __toArray() method to objects that would be called on cast to array

- Thread: https://externals.io/message/98539
- Original archive: https://news-web.php.net/php.internals/98539
- Started by bburnichon@gmail.com on 2017-03-15T17:49:40+00:00
- Messages: 27
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/98539.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #98539 — bburnichon@gmail.com — 2017-03-15T17:49:40+00:00

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

Hi all,

Looking at code of PHPUnit, I stumbled upon an inconsistent array
conversion:

------
/**
 * @param ArrayAccess|array $other
 */
function evaluate($other)
{
  // type cast $other as an array to allow
  //support in standard array functions.
  if ($other instanceof ArrayAccess) {
    $data = (array) $data;
  }

  $patched = array_replace_recursive($other, $this->subset);

  // ...
}
-----

This would work only for `ArrayAccess` implementations extending
`ArrayObject` as shown by https://3v4l.org/ti4aY

Looking at the manual
http://php.net/manual/en/language.types.array.php#language.types.array.casting
,
it seems `ArrayObject` class does not comply to array casting because
integer public properties could also be retrieved. Some tests showed that
regular class always have string keys even when a `$key = 0; $this->{$key}
= 'avalue';` is called. In this case, `var_export((array)$object);` returns
`array('0' => 'avalue')` (Notice the quote around key 0 -
https://3v4l.org/6QW70)

What do you think of adding an optional `__toArray()` method to classes
which would default to current behavior but would allow specifying
behavior. The way of internal `__toString()` method and could explain
inconsistency of the `ArrayObject` class?

Regards,

Benoît Burnichon

---

## #98540 — Ryan Pallas — 2017-03-15T18:13:58+00:00

URL: https://externals.io/message/98539#98540 · In reply to: #98539

On Wed, Mar 15, 2017 at 11:49 AM, Benoît Burnichon <bburnichon@gmail.com>
wrote:

> Hi all,
>
> Looking at code of PHPUnit, I stumbled upon an inconsistent array
> conversion:
>
> ------
> /**
>  * @param ArrayAccess|array $other
>  */
> function evaluate($other)
> {
>   // type cast $other as an array to allow
>   //support in standard array functions.
>   if ($other instanceof ArrayAccess) {
>     $data = (array) $data;
>   }
>
>   $patched = array_replace_recursive($other, $this->subset);
>
>   // ...
> }
> -----
>
> This would work only for `ArrayAccess` implementations extending
> `ArrayObject` as shown by https://3v4l.org/ti4aY
>
> Looking at the manual
> http://php.net/manual/en/language.types.array.php#
> language.types.array.casting
> ,
> it seems `ArrayObject` class does not comply to array casting because
> integer public properties could also be retrieved. Some tests showed that
> regular class always have string keys even when a `$key = 0; $this->{$key}
> = 'avalue';` is called. In this case, `var_export((array)$object);` returns
> `array('0' => 'avalue')` (Notice the quote around key 0 -
> https://3v4l.org/6QW70)
>
> What do you think of adding an optional `__toArray()` method to classes
> which would default to current behavior but would allow specifying
> behavior. The way of internal `__toString()` method and could explain
> inconsistency of the `ArrayObject` class?
>

I like the idea kind of, but would this remove the ability to cast to array
all classes not implementing __toArray, as is the case with __toString?
This would be a HUGE BC if so:

$ php -r 'class Foo {public $foo = "foobar";} var_dump((array) (new Foo));'
array(1) {
  ["foo"]=>
  string(6) "foobar"
}
$ php -r 'class Foo {public $foo = "foobar";} var_dump((string) (new Foo));'
PHP Recoverable fatal error:  Object of class Foo could not be converted to
string in Command line code on line 1
$ php -v
PHP 7.1.2 (cli) (built: Feb 27 2017 00:02:44) ( ZTS )


> Regards,
>
> Benoît Burnichon

---

## #98541 — bburnichon@gmail.com — 2017-03-15T18:31:50+00:00

URL: https://externals.io/message/98539#98541 · In reply to: #98540

> I like the idea kind of, but would this remove the ability to cast to
> array all classes not implementing __toArray, as is the case with
> __toString? This would be a HUGE BC if so:
>
> $ php -r 'class Foo {public $foo = "foobar";} var_dump((array) (new Foo));'
> array(1) {
>   ["foo"]=>
>   string(6) "foobar"
> }
> $ php -r 'class Foo {public $foo = "foobar";} var_dump((string) (new
> Foo));'
> PHP Recoverable fatal error:  Object of class Foo could not be converted
> to string in Command line code on line 1
> $ php -v
> PHP 7.1.2 (cli) (built: Feb 27 2017 00:02:44) ( ZTS )
>

No. For me, classes not implementing __toArray() should keep current
behavior. That was what I had in mind when I wrote that __toArray() should
have a default standard implementation.

Same restrictions could be applied to this magic method:
http://us3.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring

By this, I mean, no exceptions should be thrown in this method and return
value MUST be an array.

---

## #98542 — Andrey Andreev — 2017-03-15T18:39:00+00:00

URL: https://externals.io/message/98539#98542 · In reply to: #98541

Hi,


> > […]
>
> No. For me, classes not implementing __toArray() should keep current
> behavior. That was what I had in mind when I wrote that __toArray() should
> have a default standard implementation.
>
> Same restrictions could be applied to this magic method:
> http://us3.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring
>
> By this, I mean, no exceptions should be thrown in this method and return
> value MUST be an array.

Exceptions cannot be thrown from inside __toString() because that's
hard to implement; it's not a "feature" that anybody wanted.

Cheers,
Andrey.

---

## #98543 — bburnichon@gmail.com — 2017-03-15T19:29:21+00:00

URL: https://externals.io/message/98539#98543 · In reply to: #98542

>
> > […]
> http://us3.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring
> > […]
>
> Exceptions cannot be thrown from inside __toString() because that's
> hard to implement; it's not a "feature" that anybody wanted.
>

That is why I said 'could be applied'. I don't currently know whether this
will be hard to implement. Maybe these restrictions are unneeded but I
prefer to be stricter than necessary as it generally make it easier to
implement. But thanks for the clarification.

---

## #98544 — Fleshgrinder — 2017-03-15T19:36:35+00:00

URL: https://externals.io/message/98539#98544 · In reply to: #98539

> Hi all,
> 
> Looking at code of PHPUnit, I stumbled upon an inconsistent array
> conversion:
> 
> ------
> /**
>  * @param ArrayAccess|array $other
>  */
> function evaluate($other)
> {
>   // type cast $other as an array to allow
>   //support in standard array functions.
>   if ($other instanceof ArrayAccess) {
>     $data = (array) $data;
>   }
> 
>   $patched = array_replace_recursive($other, $this->subset);
> 
>   // ...
> }
> -----
> 
> This would work only for `ArrayAccess` implementations extending
> `ArrayObject` as shown by https://3v4l.org/ti4aY
> 
> Looking at the manual
> http://php.net/manual/en/language.types.array.php#language.types.array.casting
> ,
> it seems `ArrayObject` class does not comply to array casting because
> integer public properties could also be retrieved. Some tests showed that
> regular class always have string keys even when a `$key = 0; $this->{$key}
> = 'avalue';` is called. In this case, `var_export((array)$object);` returns
> `array('0' => 'avalue')` (Notice the quote around key 0 -
> https://3v4l.org/6QW70)
> 
> What do you think of adding an optional `__toArray()` method to classes
> which would default to current behavior but would allow specifying
> behavior. The way of internal `__toString()` method and could explain
> inconsistency of the `ArrayObject` class?
> 
> Regards,
> 
> Benoît Burnichon
> 

Wouldn't it also be possible to add a `Arrayable` interface with a
totally normal `toArray` method? This would avoid littering code bases
even more with feature detection code like
`method_exists($x, '__toString')`.

There would be nothing special to do here, other than implementing that
interface for all core classes for which it makes sense.

The only question that remains (regardless of the strategy) would be: is
it iterable? I guess the answer "yes" is obvious here. Hence,
`Arrayable` would need to extend at least `Traversable`.

-- 
Richard "Fleshgrinder" Fussenegger

---

## #98545 — Marco Pivetta — 2017-03-15T20:41:15+00:00

URL: https://externals.io/message/98539#98545 · In reply to: #98539

This is a BC break due to the fact that the `(array)` cast is used to
extract property information from private properties in library code.



> Hi all,
>
> Looking at code of PHPUnit, I stumbled upon an inconsistent array
> conversion:
>
> ------
> /**
>  * @param ArrayAccess|array $other
>  */
> function evaluate($other)
> {
>   // type cast $other as an array to allow
>   //support in standard array functions.
>   if ($other instanceof ArrayAccess) {
>     $data = (array) $data;
>   }
>
>   $patched = array_replace_recursive($other, $this->subset);
>
>   // ...
> }
> -----
>
> This would work only for `ArrayAccess` implementations extending
> `ArrayObject` as shown by https://3v4l.org/ti4aY
>
> Looking at the manual
> http://php.net/manual/en/language.types.array.php#
> language.types.array.casting
> ,
> it seems `ArrayObject` class does not comply to array casting because
> integer public properties could also be retrieved. Some tests showed that
> regular class always have string keys even when a `$key = 0; $this->{$key}
> = 'avalue';` is called. In this case, `var_export((array)$object);` returns
> `array('0' => 'avalue')` (Notice the quote around key 0 -
> https://3v4l.org/6QW70)
>
> What do you think of adding an optional `__toArray()` method to classes
> which would default to current behavior but would allow specifying
> behavior. The way of internal `__toString()` method and could explain
> inconsistency of the `ArrayObject` class?
>
> Regards,
>
> Benoît Burnichon

---

## #98548 — Kalle Sommer Nielsen — 2017-03-15T22:27:36+00:00

URL: https://externals.io/message/98539#98548 · In reply to: #98545

Hi

2017-03-15 21:41 GMT+01:00 Marco Pivetta <ocramius@gmail.com>:
> This is a BC break due to the fact that the `(array)` cast is used to
> extract property information from private properties in library code.

Yep, but then again that is more of an
undocumented-not-really-supported case afair, if anything then
Reflection should have the APIs to officially allow that, although I
am still skeptic of this.


-- 
regards,

Kalle Sommer Nielsen
kalle@php.net

---

## #98549 — Marco Pivetta — 2017-03-15T22:33:28+00:00

URL: https://externals.io/message/98539#98549 · In reply to: #98548

It's the only way to distinguish between set and unset properties. Also the
only way to get all properties from an instance of an inheritance tree.
Also, it's covered by tests that were explicitly added to prevent
regressions on this.

Same as all similar discussions before this one: need an alternative way to
do things before proposing a BC break.



> Hi
>
> 2017-03-15 21:41 GMT+01:00 Marco Pivetta <ocramius@gmail.com>:
> > […]
>
> Yep, but then again that is more of an
> undocumented-not-really-supported case afair, if anything then
> Reflection should have the APIs to officially allow that, although I
> am still skeptic of this.
>
>
> --
> regards,
>
> Kalle Sommer Nielsen
> kalle@php.net

---

## #98550 — Ryan Pallas — 2017-03-15T22:36:24+00:00

URL: https://externals.io/message/98539#98550 · In reply to: #98549

> It's the only way to distinguish between set and unset properties. Also the
> only way to get all properties from an instance of an inheritance tree.
> Also, it's covered by tests that were explicitly added to prevent
> regressions on this.
>
> Same as all similar discussions before this one: need an alternative way to
> do things before proposing a BC break.
>
>
As mentioned in previous mails - the intent isn't to change existing
behaviour, but to provide a way for a class to override the default
behaviour. As long as those classes you are casting to array don't
implement __toArray they will behave exactly as they always have. The only
concern then, is that you might be relying on a library to not implement
that function on a class you are casting.


> 
>
> > […]

---

## #98551 — Marco Pivetta — 2017-03-15T22:40:47+00:00

URL: https://externals.io/message/98539#98551 · In reply to: #98550

Which is precisely the BC break: such a library would now throw an
exception "unsupported class X" when `__toString` is implemented. Also,
such a library would break silently when this feature is implemented.

Much like the already very broken `__debugInfo()`, this automatic type-cast
is a giga-Joule footgun.



>
>
> 
>
> > […]
> As mentioned in previous mails - the intent isn't to change existing
> behaviour, but to provide a way for a class to override the default
> behaviour. As long as those classes you are casting to array don't
> implement __toArray they will behave exactly as they always have. The only
> concern then, is that you might be relying on a library to not implement
> that function on a class you are casting.
>
>
> > […]

---

## #98552 — Fleshgrinder — 2017-03-15T22:50:58+00:00

URL: https://externals.io/message/98539#98552 · In reply to: #98551

> Which is precisely the BC break: such a library would now throw an
> exception "unsupported class X" when `__toString` is implemented. Also,
> such a library would break silently when this feature is implemented.
> 
> Much like the already very broken `__debugInfo()`, this automatic type-cast
> is a giga-Joule footgun.

I have to backup Ocramius here. As I already wrote, let's go for a
totally normal implementation without any kind of magic. This is the
only way to be safe, and the only way without adding more weirdness. All
this special casing and magic only creates problems in short and long term.

-- 
Richard "Fleshgrinder" Fussenegger

---

## #98553 — bburnichon@gmail.com — 2017-03-15T22:51:03+00:00

URL: https://externals.io/message/98539#98553 · In reply to: #98551

I was not aware of all that being the only way to distinguish set/unset
properties.
I do agree with Marco on this point that it would be a BC break. It was not
my intention at all and did not foresee that.

Up to now, I thought array casting objects was doing the same as for scalar
values. I was wrong and I think many other developers around too.



> Which is precisely the BC break: such a library would now throw an
> exception "unsupported class X" when `__toString` is implemented. Also,
> such a library would break silently when this feature is implemented.
>
> Much like the already very broken `__debugInfo()`, this automatic
> type-cast is a giga-Joule footgun.

---

## #98554 — Ryan Pallas — 2017-03-15T22:57:18+00:00

URL: https://externals.io/message/98539#98554 · In reply to: #98551

Which is precisely the BC break: such a library would now throw an
exception "unsupported class X" when `__toString` is implemented. Also,
such a library would break silently when this feature is implemented.

Much like the already very broken `__debugInfo()`, this automatic type-cast
is a giga-Joule footgun.


First please stop top posting.

Second bc means that if no code changes no functionality is changed. That's
exactly what we're talking about. If the class doesn't change neither does
the functionality. So unless classes already have __toArray they will not
change in behaviour.

As pointed out in answer to my question earlier a class with no code change
would see no change in casting behaviour. Only if new method is implemented
will behaviour change. How does that not maintain bc?




>
>
> 
>
> > […]
> As mentioned in previous mails - the intent isn't to change existing
> behaviour, but to provide a way for a class to override the default
> behaviour. As long as those classes you are casting to array don't
> implement __toArray they will behave exactly as they always have. The only
> concern then, is that you might be relying on a library to not implement
> that function on a class you are casting.
>
>
> > […]

---

## #98555 — Marco Pivetta — 2017-03-15T23:03:28+00:00

URL: https://externals.io/message/98539#98555 · In reply to: #98554

Hi Ryan,

I'm top-posting because I'm writing from a phone. I always do and I also
stopped caring for top-posters myself because it's fairly normal, plus
modern email clients deal with it. If I can write a damn mail from a phone
keyboard because I don't have any better right now, then you can probably
use the scroll wheel once on your pc too.

The BC break applies to API that accepts `object` (any object). Such API is
common in library code in frameworks, data-mappers, etc.

Such code would not work anymore for cases where the magic method is
implemented, adding either exceptions (forcing a library-level BC break) or
simply by causing the existing stable versions of these libs to be
incompatible with newer php versions.








Which is precisely the BC break: such a library would now throw an
exception "unsupported class X" when `__toString` is implemented. Also,
such a library would break silently when this feature is implemented.

Much like the already very broken `__debugInfo()`, this automatic type-cast
is a giga-Joule footgun.


First please stop top posting.

Second bc means that if no code changes no functionality is changed. That's
exactly what we're talking about. If the class doesn't change neither does
the functionality. So unless classes already have __toArray they will not
change in behaviour.

As pointed out in answer to my question earlier a class with no code change
would see no change in casting behaviour. Only if new method is implemented
will behaviour change. How does that not maintain bc?




>
>
> 
>
> > […]
> As mentioned in previous mails - the intent isn't to change existing
> behaviour, but to provide a way for a class to override the default
> behaviour. As long as those classes you are casting to array don't
> implement __toArray they will behave exactly as they always have. The only
> concern then, is that you might be relying on a library to not implement
> that function on a class you are casting.
>
>
> > […]

---

## #98556 — Ryan Pallas — 2017-03-15T23:27:42+00:00

URL: https://externals.io/message/98539#98556 · In reply to: #98555

Hi Ryan,

I'm top-posting because I'm writing from a phone. I always do and I also
stopped caring for top-posters myself because it's fairly normal, plus
modern email clients deal with it. If I can write a damn mail from a phone
keyboard because I don't have any better right now, then you can probably
use the scroll wheel once on your pc too.

I'll just say this: I'm also on a mobile device. Don't make assumptions.


The BC break applies to API that accepts `object` (any object). Such API is
common in library code in frameworks, data-mappers, etc.

Such code would not work anymore for cases where the magic method is
implemented, adding either exceptions (forcing a library-level BC break) or
simply by causing the existing stable versions of these libs to be
incompatible with newer php versions.


I must misunderstand what constitutes an BC break. I thought a BC break was
when identical code produced different results. But you're saying its a
break because someone could change their code to use a new feature and
break their use of your library?










Which is precisely the BC break: such a library would now throw an
exception "unsupported class X" when `__toString` is implemented. Also,
such a library would break silently when this feature is implemented.

Much like the already very broken `__debugInfo()`, this automatic type-cast
is a giga-Joule footgun.


First please stop top posting.

Second bc means that if no code changes no functionality is changed. That's
exactly what we're talking about. If the class doesn't change neither does
the functionality. So unless classes already have __toArray they will not
change in behaviour.

As pointed out in answer to my question earlier a class with no code change
would see no change in casting behaviour. Only if new method is implemented
will behaviour change. How does that not maintain bc?




>
>
> 
>
> > […]
>
> Also, it's covered by tests that were explicitly added to prevent
> > […]
> As mentioned in previous mails - the intent isn't to change existing
> behaviour, but to provide a way for a class to override the default
> behaviour. As long as those classes you are casting to array don't
> implement __toArray they will behave exactly as they always have. The only
> concern then, is that you might be relying on a library to not implement
> that function on a class you are casting.
>
>
> > […]
> Does it not through get properties?

>
> > […]

---

## #98557 — Marco Pivetta — 2017-03-15T23:33:10+00:00

URL: https://externals.io/message/98539#98557 · In reply to: #98556

Correct: passing an object that implements `__toArray()` to an API that
uses an `(array)` cast internally will break or misbehave, if this feature
is added to the language.



>
>
> 
>
> Hi Ryan,
>
> I'm top-posting because I'm writing from a phone. I always do and I also
> stopped caring for top-posters myself because it's fairly normal, plus
> modern email clients deal with it. If I can write a damn mail from a phone
> keyboard because I don't have any better right now, then you can probably
> use the scroll wheel once on your pc too.
>
> I'll just say this: I'm also on a mobile device. Don't make assumptions.
>
>
> The BC break applies to API that accepts `object` (any object). Such API
> is common in library code in frameworks, data-mappers, etc.
>
> Such code would not work anymore for cases where the magic method is
> implemented, adding either exceptions (forcing a library-level BC break) or
> simply by causing the existing stable versions of these libs to be
> incompatible with newer php versions.
>
>
> I must misunderstand what constitutes an BC break. I thought a BC break
> was when identical code produced different results. But you're saying its a
> break because someone could change their code to use a new feature and
> break their use of your library?
>
>
>
>
> 
>
>
>
> 
>
> Which is precisely the BC break: such a library would now throw an
> exception "unsupported class X" when `__toString` is implemented. Also,
> such a library would break silently when this feature is implemented.
>
> Much like the already very broken `__debugInfo()`, this automatic
> type-cast is a giga-Joule footgun.
>
>
> First please stop top posting.
>
> Second bc means that if no code changes no functionality is changed.
> That's exactly what we're talking about. If the class doesn't change
> neither does the functionality. So unless classes already have __toArray
> they will not change in behaviour.
>
> As pointed out in answer to my question earlier a class with no code
> change would see no change in casting behaviour. Only if new method is
> implemented will behaviour change. How does that not maintain bc?
>
>
> 
>
> > […]
>
> > […]

---

## #98558 — Andrey Andreev — 2017-03-15T23:53:24+00:00

URL: https://externals.io/message/98539#98558 · In reply to: #98557

Hi,


> Correct: passing an object that implements `__toArray()` to an API that
> uses an `(array)` cast internally will break or misbehave, if this feature
> is added to the language.
>

I'm not particularly interested in the idea anyway, but if you change
code and *then* something changes - that's not a BC break.

Also, I have to agree with Fleshgrinder on that we don't need more
magic methods and using an interface would be much cleaner.

Cheers,
Andrey.

---

## #98559 — Marco Pivetta — 2017-03-16T00:01:22+00:00

URL: https://externals.io/message/98539#98559 · In reply to: #98558

What changes is the interface of the `(array)` operator.



> Hi,
>
> 
> > […]
> feature
> > […]
>
> I'm not particularly interested in the idea anyway, but if you change
> code and *then* something changes - that's not a BC break.
>
> Also, I have to agree with Fleshgrinder on that we don't need more
> magic methods and using an interface would be much cleaner.
>
> Cheers,
> Andrey.

---

## #98560 — Andrey Andreev — 2017-03-16T00:22:54+00:00

URL: https://externals.io/message/98539#98560

What changes is the interface of the `(array)` operator.


I understand what you mean, I just disagree that it constitutes a BC break
in the sense that no existing code would break/misbehave by simply updating
to a PHP version including the feature.

That's just the only sane criteria by which to label changes as BC breaks,
because if we don't stick to that, there'd be an argument to be made that
literally every change is. And that would mean the term loses its meaning
and becomes useless.

Cheers,
Andrey.

---

## #98561 — Marco Pivetta — 2017-03-16T00:32:57+00:00

URL: https://externals.io/message/98539#98561 · In reply to: #98560

As just told: existing code hinting against generic `object` breaks. This
is a change in semantics in an existing operator. It is just sane to
consider it a BC break, since the operator cannot be relied upon for a
certain family of problems: invent a new operator.

That's basically the path to follow anytime something could be overloaded,
but shouldn't due to BC constraints.





>
>
> 
>
> What changes is the interface of the `(array)` operator.
>
>
> I understand what you mean, I just disagree that it constitutes a BC break
> in the sense that no existing code would break/misbehave by simply updating
> to a PHP version including the feature.
>
> That's just the only sane criteria by which to label changes as BC breaks,
> because if we don't stick to that, there'd be an argument to be made that
> literally every change is. And that would mean the term loses its meaning
> and becomes useless.
>
> Cheers,
> Andrey.

---

## #98563 — Sebastian Bergmann — 2017-03-16T05:46:40+00:00

URL: https://externals.io/message/98539#98563 · In reply to: #98549

Am 15.03.2017 um 23:33 schrieb Marco Pivetta:
> Also the only way to get all properties from an instance of an inheritance tree.

I use the Reflection API in
https://github.com/sebastianbergmann/object-reflector/blob/master/src/ObjectReflector.php#L24
for retrieving inherited non-public attributes from an object by
traversing the inheritance chain using while ($reflector =
$reflector->getParentClass()).

I wish the ReflectionObject would allow access to all attributes of an
object no matter whether an attribute is declared in the class of the
object or in a parent class.

---

## #98564 — bburnichon@gmail.com — 2017-03-16T06:27:59+00:00

URL: https://externals.io/message/98539#98564 · In reply to: #98561

FWIW I do prefer having a specific interface to do such conversions. I was
just trying to find a way to have list-like or Traversable objects ability
to be converted to plain array. This brings more problems than actually
solving. I will just stop thinking about this and can give this thread as
reference of why this feature would be a bad idea.

---

## #98565 — Andrea Faulds — 2017-03-16T09:16:24+00:00

URL: https://externals.io/message/98539#98565 · In reply to: #98539

Hi Benoît,

Benoît Burnichon wrote:
> Looking at the manual
> http://php.net/manual/en/language.types.array.php#language.types.array.casting
> ,
> it seems `ArrayObject` class does not comply to array casting because
> integer public properties could also be retrieved. Some tests showed that
> regular class always have string keys even when a `$key = 0; $this->{$key}
> = 'avalue';` is called. In this case, `var_export((array)$object);` returns
> `array('0' => 'avalue')` (Notice the quote around key 0 -
> https://3v4l.org/6QW70)

I'm not sure what specifically you see to be the problem, but I think it 
might be worth directing you to my RFC which relates to this:

https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts

-- 
Andrea Faulds
https://ajf.me/

---

## #98567 — Marco Pivetta — 2017-03-16T14:01:55+00:00

URL: https://externals.io/message/98539#98567 · In reply to: #98560

Since some folks keep banging on "it's not a BC break", I propose a
challenge in fixing this particular BC break example (reads: find a
different way to make it work, and no warnings/notices allowed):

I made a very simplistic example of where `__toArray` will break existing
API that currently literally accepts any kind of object:
https://3v4l.org/Tj0GH

For reference, here's the code copy:

<?php

class Foo
{
    public $bar;
    public $baz;
    public $taz;
}

// please note that this API has a signature of `object $object` - any
object allowed.
function unsetProperties($object) {
    // we ignore inheritance and private properties for simplicity
    return array_diff(
        array_map(
            function (\ReflectionProperty $p) : string {
                return $p->getName();
            },
            (new ReflectionClass($object))->getProperties()
        ),
        array_keys((array) $object)
    );
}

var_dump(unsetProperties(new Foo));

$foo = new Foo;

unset($foo->baz);

var_dump(unsetProperties($foo));

There are dozens of simpler examples that break (typically, data-mapper
layers): I picked this particular one because there is no other way to
detect unset properties without causing side-effects.

If `__toArray` is implemented on an object, the API defined above will
either report all properties to be unset, or in general produce unreliable
results.

The current API specification of the `(array)` operator is as following:

 * for every SET property produce an array value, with the key being:
    * "\0" . $className . "\0" . $propertyName for private properties
    * "\0*\0" . $propertyName for protected properties
    * $propertyName for public properties
    * $propertyName for dynamically defined properties

This is a **VERY SPECIFIC** behavior that the operator currently
guarantees. Break that, and you can go on github hunting for `(array)` cast
usages.

The behavior was frozen in the current suite in https://github.com/php/php-
src/blob/8015daeddb7bb4951c808fa253b97753787fb0ea/tests/classes/
array_conversion_keys.phpt (although, now that I notice, doesn't cover
dynamically defined properties - should add that).

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

---

Next page: https://externals.io/message/98539.md?page=2
