Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107074 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 84688 invoked from network); 13 Sep 2019 12:42:38 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 13 Sep 2019 12:42:38 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 938A62D1FFC for ; Fri, 13 Sep 2019 03:18:44 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: No Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Fri, 13 Sep 2019 03:18:43 -0700 (PDT) Received: by mail-io1-xd2c.google.com with SMTP id f12so61822135iog.12 for ; Fri, 13 Sep 2019 03:18:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindplay-dk.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=lS8/+Ez0HkPoDRQ6G4Bk8x641udQCqkK30sTPm9lowo=; b=h6HXZBiW2zuMIVlyyfQreFtsxKUS3FAdSZC8CzX2szlEe2jexWFcgnds8AhOYPM/U+ gymqXpAxmJHm00L0PcHd6nLKFGEAKwPdrLFkbPg+SpHOCf29uKOhrAjdfD56M1BmOGC4 QCZTsuiTO9VYXGA1S+vWlXP9ImEp+pCtNJZ0XswlS3HehfbYgXyasWHGvdTAX/i9QMBd Sap/lcSD7n1GSHkYGoH6/CiNHoTN3krP2J8GEfH0KVZPiUD4JncKD/8EutjAyzzeMl0o ijwJDvrbWbTDqewT9eDs4oTaL5xVhGpRcYSvl7psmMPyfhv8cCpPD19ZvmUf3cA0ICMh ogDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=lS8/+Ez0HkPoDRQ6G4Bk8x641udQCqkK30sTPm9lowo=; b=NmlN95+lTsuYt7RpUboWpDzSridCbMj6KJniRLBDhqCE2OakYbdMpJKJoXoI5fLLii VNwz8+SRhIbSOXUVdXt7oyB+82JAQIxg+ZiVYDeS08erj3C6dGcUORTwlsFz09ycc1e/ VqVZVME0kGQjSZrxzw138V3amnxq5p8ylgeKg6YcR7u2xZpQk1ntaHs+ecn+D8/jj4EY P9N1FSmXrOVPXDCMdjgQrb6naFI6VP8zR8E0NeWcBa75xJtA4kVslGix3aM4m1IHRGfM Vf2jinxWL2Wz76xcFWHjcvKRShpAESWDMRm1iqTIkkMC0YYFPEAStkuGzL7nV7fp6cJs Zy4w== X-Gm-Message-State: APjAAAUciXaDBxt5N5SSmAM5fgnYE9jQtEKZWlnDi92WjUBR6SJ2QM3B Zxa2ynqXWCU11BYIRRX+GqaWKnUredb7UDjfYIBsqsv03j3h2g== X-Google-Smtp-Source: APXvYqw/GDT16soWtIBVFN6JpdbjGIiGhHwy5EIL5W4Xhm86msTYApsjdu9NLG0DFCveFtDSBGxgnwArTkd+x4T47jc= X-Received: by 2002:a6b:8e92:: with SMTP id q140mr10574363iod.205.1568369922990; Fri, 13 Sep 2019 03:18:42 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 13 Sep 2019 12:18:35 +0200 Message-ID: To: =?UTF-8?Q?Micha=C5=82_Brzuchalski?= Cc: PHP Internals List Content-Type: multipart/alternative; boundary="00000000000022fa5b05926c9415" X-Envelope-From: Subject: Re: [PHP-DEV] [RFC] Object Initializer From: rasmus@mindplay.dk (Rasmus Schultz) --00000000000022fa5b05926c9415 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I'd like to address the examples - and why I think they don't demonstrate that this feature is really useful in practice. There are several examples similar to this one: class Car { public int $yearOfProduction; public string $vin; } This is either lacking a constructor to initialize the properties to the defined types - or it's lacking nullable type-hints. As it is, this class doesn't guarantee initialization according to it's own property type constraints. Assuming the fields of this entity are required, you would probably prefer to add a constructor - but then property initializers aren't really useful anymore. This seems to be the case for most of the examples. Models often have other constraints besides just the type - in those cases, your models would likely have private/protected fields and setter-methods that implement those constraints. The visibility example demonstrates the use of a static factory method: class Customer { private string $name =3D ''; protected ?string $email =3D null; public static function create(string $name, ?string $email =3D null): sel= f { return new self { name =3D $name, // assign private property within the same class email =3D $email, // assign protected property within the same class }; } } This may be fine for some use-cases - but many model types are only going to have one valid way to construct an instance, and constructors are the idiomatic way to do that. Unfortunately, this language feature works for the new-statement only, so a desire to use this language feature will drive architecture. All in all, I find this feature is useful or applicable only to a few, select patterns within the language - it isn't general enough. In my opinion, language features should be as general as possible - a feature like this "looks nice", being very abbreviated and clean-looking, and, as I believe the examples in the RFC itself demonstrates, this will provide the wrong kind of motivation to make what are, effectively, architectural decisions. Some models are immutable by design. Those cases don't seem to be well supported by this feature. My strong preference over this feature would be named parameters, which can provide the same abbreviated initializations, but works more consistently with the language, e.g. for all of the use-cases I cited above. It works for constructors: class Car { public int $yearOfProduction; public string $vin; public function __construct(int $yearOfProduction, string $vin) { if ($yearOfProduction < 1900 || $yearOfProduction > date("Y")) { throw new InvalidArgumentException("year of production out of range: {$yearOfProduction}"); } $this->yearOfProduction =3D $yearOfProduction; $this->vin =3D $vin; } } $car =3D new Car({ yearOfProduction =3D 1975, vin =3D "12345678"}); It works for static factory-methods: $car =3D Car::create({ yearOfProduction =3D 1975, vin =3D "12345678"}); It works for models with private/protected fields, classes with accessors, classes with validations in constructors or factory-methods, and so on. In other words, it works more generally with all common patterns and practices - in many ways, it just seems like a better fit for the language. The common criticism against named parameters, is they create coupling to parameter-names. Object initializers create coupling to property-names - if you can live with that, I don't think named parameters or object initializers are very different in that regard. The only problem I see with named parameters as an alternative to object initializers, is in terms of versioning - renaming an argument, today, is not a breaking change, and now it would be. That could be addressed, for example, by making named parameters explicit at the declaration site - for example, use curly braces in the declaration to designate named arguments: function makeCar({ int $yearOfProduction, string $vin }) { // ... } This way, adding named parameters is not a breaking change - it's an opt-in feature for those cases where it's useful and meaningful, but it's still applicable to all the patterns and cases that someone might want to use it for. Named parameters is just one possible alternative - I'm just naming it for practical comparison, to demonstrate how some features may have more general applications than others. I'd prefer to see new features that work everywhere, all the time, for everyone - and for existing code. Rather than adding more features and syntax for very specific (even relatively rare) use-cases. On Thu, Sep 12, 2019 at 4:00 PM Micha=C5=82 Brzuchalski < michal.brzuchalski@gmail.com> wrote: > Hi internals, > > I'd like to open discussion about RFC: Object Initializer. > > This proposal reduces boilerplate of object instantiation and properties > initialization in case of classes without required constructor arguments = as > a single expression with initializer block. > > https://wiki.php.net/rfc/object-initializer > > I appreciate any feedback you all can provide. > > Thanks, > -- > Micha=C5=82 Brzuchalski > brzuchal@php.net > --00000000000022fa5b05926c9415--