@IluTov "Objects are passed by reference, rather than by value". This is false / confusing, because references are something different in PHP. I'd say "The object handle is passed by value". I believe this distinction would also make it easier to explain the exact semantics further on.
Because when only the "object handle" is copied due to the by-value pass, it's more clear that the underlying object is not copied.
@IluTov When people recommend composition over inheritance, they should perhaps also mention that the visibility system in PHP is tailored for inheritance (private, protected) and not composition (no package-visibility yet).
Assuming encapsulation is important for people, sometimes I'm not even sure...
@MarkR How do you define these two terms? They're often used synonymously. In explain in the RFC why I believe readonly + withers are a bad solution to this problem, especially for growable data structures.
@IluTov DTOs + value objects? Generally the DTOs involve some element of hydration/serialization, value object is probably not the most precise term to use, but if I had an object representing, say, a prepared email message, that was passed between components, immutable, having the withers enforces the immutability, it's less of a factor on DTOs although plenty of those are read-only too, but generally they're unchanged after setup or hydration
if there was clone with { ... } i'd expect the withers to go away, although in some cases they can be handy, i.e. our value object representing an email requires setting to email + name at the same time
@LeviMorrison It's just unclear to me how that would work with constructors.
@MarkR Right, but with value types you don't really need withers or clone with at all, because it solves the problem of mutability at a distance without requiring readonly.
@MarkR wiki.php.net/rfc/structs-v2 I'm not sure if you've read it, but it's possible I don't communicate this well. I'll think about way to make this clearer.
Essentially, readonly is a way to avoid your data changing unexpectedly from other places. Arrays don't have this problem, because if somebody changes the data you shared with them, they implicitly create a copy from it. Structs work the same way. readonly is fine, except it doesn't work well with clone. Withers are tedious to write, and every mutation creates a clone of the data, even if the previous copy is discarded right after.
Structs solve all of those problems. They are immune to modification at a distance, they don't create useless copies, and they don't require boilerplate code for mutable APIs (like withers would).
Furthermore, they make growable data structures as value types possible, which was the original reason why I even looked at them.
I think it's the implicitly bit that's sitting funny with me, I can't think of ever having used something similar that wasn't explicitly copied at some point
@IluTov Oh sorry, I didn't pick up that the discussion was limited to DTOs. But yes, I agree that immutability works pretty well to solve similar problems as visibility does. :)
@MarkR Right, -> might or might not influence a 3rd party, depending on whether the type is a struct or a normal class. I'd say that the same already goes for the [] syntax due to ArrayAccess. E.g. you might expect $b = $a; $b[] = 42; assert($a !== $b); to hold, but it's not true of $a` is an object.
I would also say, does it matter? If we don't have structs, you're going to have to think about this issue regardless. At least structs can solve it for a subset of your types.
Well there's the simple case, properties only, in which case $x = $x with { ... } I would imagine could be optimized if the refcount of of $x was 1 as you'd know you were throwing away the first $x? That doesn't seem to work for anything with methods though.
I think a benefit would be that you could set a bunch of stuff at once without needing to clone each time, but it is of course a narrower scope than what you're suggesting
I'd see it as more of a code style convenience, personally. One of the reasons I like named args + promoted property constructors instead of just setting the properties is so I'm not repeating $usefulVariableName-> half a dozen times
@IluTov In my head, it doesn't interact with constructors at all. It respects scopes, though, so if there are private properties and you are try to construct one from public, it doesn't work.
@LeviMorrison It does to me as well when written with braces. That's why I argued against braces and in favor of array syntax back when it was proposed and that's also what the current RFC uses.
Not sure what's up with introducing braces for key-value pairs in PHP. That's needlessly new syntax.
It may interact with constructors in that if it has one, it it does a scope access. Meaning if there is a class with all public properties but a private constructor, then it can't be constructed with brace syntax from public nor protected, just private. The constructor isn't called, though.
Array syntax is the right choice for clone with, though, because it naturally extends to making the right side of the with dynamic and passing an actual array.
@LeviMorrison What I meant is: If you're not cloning an existing value (so, without ...$foo), how do you create a new value, assign the properties, while ensuring the object is properly initialized, without calling __construct?
@LeviMorrison That's wasn't really the point. You could have private properties being initialized in the constructor. Not calling the constructor breaks assumptions within the object that it is properly initialized. In comparison, Rust forces you to specify all the fields of a struct. But even then, your constuctor may sanitize the params in some way, and then assume that they are properly sanitized.
@IluTov No, you misunderstand. That's not the implementation. I'm saying that your "access control" didn't actually restrict anything, and this wouldn't be any different?
Isn't that what we do with default args on property promotion anyway? If you have a constructor, you must use the constructor, if you don't have a constructor, allow creating using new property struct syntax, and throw an error if it doesn't provide every property that doesn't have a default
@IluTov The constructor uppercases things, right? However, the property is public and writeable. What stops you from changing it to a non-upper case string? Nothing.
@LeviMorrison Right. I understand that part. But even if you change them after, how do you call the constructor if you don't know what values to pass to them? You could try to figure out from the initializer list, but nothing guarantees that they'll match the constructor args.
Not calling the constructor breaks interior assumptions, e.g. constructor initialized private fields, or setup of other sorts. Some serialization libraries use newInstanceWithoutConstructor, but they need to be careful to leave the object in a valid state when they're done. Not calling constructors would also mean that adding any such constructor code that is expected to run would become a breaking change.
Users would also need to look at the implementation of __construct to understand whether using the syntax is safe.
But if some 3rd party uses Foo {} without initializing the private field, the object itself doesn't expect that the constructor wasn't called. All code accessing the private field will now need to deal with !isset.
@LeviMorrison Ok. So if the struct declares a private field, or maybe more appropriately, when it declares a constructor at all, this syntax breaks. That just significantly restricts its use, and becomes a BC break when a struct adds a constructor.
@IluTov A constructor can be present. Its visibility is respected. Right now I'm struggling to find cases where you have public properties with a public constructor that are broken, aside from things like readonly as mentioned previously. The public property generally means anything type-safe can be written to it at any point so... effectively ignoring the constructor (although again, respecting its visibility) and just writing those properties should be allowed, yes?
@LeviMorrison Maybe I didn't think it through, but stdClass just remains a reference type. An alternative with value semantics could be useful, although at that point you could probably just use an array.
@LeviMorrison Right, fully public properties are not really my concern. They may change arbitrarily.
@IluTov I meant that in two RFCs that talk about class naming and specifically their casing... how did neither one mention stdClass which doesn't seem to follow any convention at all?
@IluTov No worries. I switched topics suddenly lol
@IluTov So what things are you concerned about? Doesn't need to be struct object specific, class objects should support this too. I still feel like I don't grasp your arguments.
@LeviMorrison If the syntax is allowed in the presence of __construct, if breaks interior assumptions. If it doesn't, it introduces BC breaks when adding constructors, and is just less useful overall.
@LeviMorrison Private properties will remain uninitialized. But you already said you'd disable the syntax in that case, so only really the latter half of my last message is relevant.
To be clear, the class itself could use this syntax to initialize private properties, because they have access to them. It's not just "ah, there's a non-public property, can't use this feature on this object!" It's visibility based to the scope calling it.
@IluTov To be sure, you mean that there is a class with only public properties today, and tomorrow, you add a private property. This is now a BC break because callers which used brace initialization are now broken. Something like this?
@IluTov I believe this is a valid complaint, and one written about in the Rust community. Adding the first private field is a BC break. You'll see sometimes where people add a private field that does nothing just to avoid this kind of thing.
@LeviMorrison I see. I was wondering how Rust solves this. In practice, I would expect most libraries to create new initializers for their structs? I have no idea though, I have never done anything productive with Rust.