On Fri, 15 Mar 2019 at 03:48, Kenneth Ellis McCall xellisx@gmail.com
wrote:
PHP has many data types, but does not offer something that is the
equivalent to the C struct data type.The purpose of this RFC is to propose a data type of 'struct', which
would be a strictly typed, immutable data structure that resembles a mix
of a class and an array.
Hi Kenneth,
I'm generally in favour of adding some kind of struct type to PHP, but I
think "like in C" and "a mix of a class and an array" doesn't really sell
what it is and why we want it. The important factors for me are:
- Statically typed, unlike an array
- Copy-on-write like an array, not pass-by-pointer like an object
A truly immutable type, in the sense that its properties can never be
changed after construction, seems like a slightly different proposition - C
structs certainly aren't immutable in that sense - but maybe that's not
what you meant.
I'd always pictured it much more like a class definition, rather than the
array-like syntax you show:
struct Foo {
boolean $prop1;
int $prop2;
}
This is partly influenced by C#, which has structs as a special type of
object, complete with constructors, methods, etc, which feels more powerful
than an array with types:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/struct
For the same reason, I would expect properties to be accessed as
$foo->prop1, not $foo['prop1'] - these are declared properties like on an
object, not keys in an expandable dictionary.
Regards,
Rowan Collins
[IMSoP]