Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-16 Thread Marco Pivetta
On Thu, Mar 16, 2017 at 3:34 PM, Marcio Almada wrote: > 2017-03-16 11:01 GMT-03:00 Marco Pivetta : > >> 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/noti

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-16 Thread Marcio Almada
2017-03-16 11:01 GMT-03:00 Marco Pivetta : > 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 `_

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-16 Thread Marco Pivetta
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 lite

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Benoît Burnichon
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 ref

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Sebastian Bergmann
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 attribut

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Marco Pivetta
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 foll

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Andrey Andreev
On Mar 16, 2017 2:01 AM, "Marco Pivetta" wrote: 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.

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Marco Pivetta
What changes is the interface of the `(array)` operator. On 16 Mar 2017 12:53 a.m., "Andrey Andreev" wrote: > Hi, > > On Thu, Mar 16, 2017 at 1:33 AM, Marco Pivetta wrote: > > Correct: passing an object that implements `__toArray()` to an API that > > uses an `(array)` cast internally will brea

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Andrey Andreev
Hi, On Thu, Mar 16, 2017 at 1:33 AM, Marco Pivetta wrote: > 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 i

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Marco Pivetta
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. On 16 Mar 2017 12:27 a.m., "Ryan Pallas" wrote: > > > On Mar 15, 2017 5:03 PM, "Marco Pivetta" wrote: > > Hi Ryan, > > I'm

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Ryan Pallas
On Mar 15, 2017 5:03 PM, "Marco Pivetta" wrote: 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 d

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Marco Pivetta
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 proba

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Ryan Pallas
On Mar 15, 2017 16:40, "Marco Pivetta" wrote: 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 `__debu

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Benoît Burnichon
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 wr

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Fleshgrinder
On 3/15/2017 11:40 PM, Marco Pivetta wrote: > 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 `

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Marco Pivetta
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-Jou

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Ryan Pallas
On Wed, Mar 15, 2017 at 4:33 PM, Marco Pivetta wrote: > 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.

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Marco Pivetta
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 altern

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Kalle Sommer Nielsen
Hi 2017-03-15 21:41 GMT+01:00 Marco Pivetta : > 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 Refle

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Marco Pivetta
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. On 15 Mar 2017 6:50 p.m., "Benoît Burnichon" wrote: > Hi all, > > Looking at code of PHPUnit, I stumbled upon an inconsistent array > conversion: > > --

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Fleshgrinder
On 3/15/2017 6:49 PM, Benoît Burnichon 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 sta

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Benoît Burnichon
> > > 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 i

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Andrey Andreev
Hi, On Wed, Mar 15, 2017 at 8:31 PM, Benoît Burnichon wrote: >> 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

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Benoît Burnichon
> 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"]=> > str

Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Ryan Pallas
On Wed, Mar 15, 2017 at 11:49 AM, Benoît Burnichon 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 > //su

[PHP-DEV] Add __toArray() method to objects that would be called on cast to array

2017-03-15 Thread Benoît Burnichon
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