Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107084 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31445 invoked from network); 14 Sep 2019 11:11:07 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 14 Sep 2019 11:11:07 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id B1A4D2D2007 for ; Sat, 14 Sep 2019 01:47:28 -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-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) (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 ; Sat, 14 Sep 2019 01:47:28 -0700 (PDT) Received: by mail-pg1-x542.google.com with SMTP id z12so5004774pgp.9 for ; Sat, 14 Sep 2019 01:47:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=ksLe7vSlxvLpebiuLBLDhZbN4VIbKSeNXDV5Ae1ZYck=; b=vjEQKmX7L2xdpgrTldkT9AsNl/VKELj0UEiRC/qXfbsoOtBDaiIvHnC5XLENEnYt4G mUFl1fLaDi1tmD5GZT/CSk2aQ31By0zZz5Y3ABHuE9y7ayqr67gUBRQb6haAN1lhvobl cDIsu3wceq894drf/7MsI+GyrKXledoUiajL+1oqM9Ox3FYv5DIS/NCH71noWJx6QhiB qgSz2ZibWO/6EFiEdyHgMIxhng33W+iY9+aSv9MSgmdg1MKrYDTuTsTXAgWOqYHQXJbb FTbnqC/gMj4P/yFrUDgIUPGzSlenhJ7zQIaf9MWaYYrHeaEBrkj8KWwWj75gOG8GS6vG gOPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=ksLe7vSlxvLpebiuLBLDhZbN4VIbKSeNXDV5Ae1ZYck=; b=IWxrJ4HfIzL3Ml/yp/1gJ6rCgxQUXdajgwcnIEbWToE/Os2N6f+sftrI0Zj8s5mCEb 7BCTNP1ot8ewFAjHEYQAok5aoCB4nMa1elmA4o9O+C4OiUC7iA5EV2RBXdBQQUsl2151 vNSRSTu5tyqS7u661gHnZheFJHW42MdootbS7hjNQ391+KZW4UdR95PS8jI++ggTzP7o v8foE1qRE4MqpNIe+Sdd9tGzEv1qvwn2sBDYzSo93cp0sfudPWuFbUxKBzDv3j/jl7h7 gurygsl/yDajBXGCA2Q7TYCOuVGLCltU1u7Ter0L/QiS4xIesl3Abr9ybcyDDI6oD3JU VxhQ== X-Gm-Message-State: APjAAAU2f7GIorZl0aJG9tPZ1lV9TraIxH8gAv7fVBy9xGSwtWNguFM3 ZytO5JE74CsP9VcNmTdsViLs19SxyLRJSQ== X-Google-Smtp-Source: APXvYqzvUuWEyW6YZ5sHMYF/Rq9daKLnDJQ3iGlx/G1dkPkMxvMlioxbrRl+9XKyFTk0tPBXzJb9UA== X-Received: by 2002:a65:4507:: with SMTP id n7mr16681345pgq.86.1568450847350; Sat, 14 Sep 2019 01:47:27 -0700 (PDT) Received: from [10.20.3.119] ([198.233.189.11]) by smtp.gmail.com with ESMTPSA id y15sm37255373pfp.111.2019.09.14.01.47.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 14 Sep 2019 01:47:26 -0700 (PDT) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_404C7241-22CE-4971-9EA2-C51CAEDA2E49" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Date: Sat, 14 Sep 2019 01:47:25 -0700 In-Reply-To: <0275793C-EC66-4693-B74F-B794E6FF0AC6@newclarity.net> Cc: =?utf-8?Q?Micha=C5=82_Brzuchalski?= , PHP Internals List To: Rasmus Schultz References: <0275793C-EC66-4693-B74F-B794E6FF0AC6@newclarity.net> X-Mailer: Apple Mail (2.3445.104.11) X-Envelope-From: Subject: Re: [PHP-DEV] [RFC] Object Initializer From: mike@newclarity.net (Mike Schinkel) --Apple-Mail=_404C7241-22CE-4971-9EA2-C51CAEDA2E49 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Sep 13, 2019, at 3:18 AM, Rasmus Schultz > wrote: >=20 > 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.=20 I think this is a false dichotomy; I can see benefit to having = initializers in addition to constructors, even for the same classes. However, Object Initializers might be best when paired with a new magic = method which let us call __init(), and a required modifier for = properties. =20 Using your Customer example, the __init() would be automatically called = after __construct(): class Customer { private string $name =3D ''; required protected ?string $email =3D null; public function __construct(string $name, ?string $email =3D null) { $this->name =3D $name; $this->email =3D $email; } public function __init() =20 { $this->name =3D ucwords($this->name); $this->email =3D sanitize_email($this->email); } } With __init() a developer could separate capturing construct parameters = from initializing required fields, but the required modifier would allow = developers to indicate which properties should be required.=20 And it might even make sense to throw an error if an object initializer = does not have an __init() method. Requiring __init() would ensure that = classes not designed to work with initializers would never be = initialized without required fields set. When creating an object using object initialization I think it should = just bypass and only call __init(). PHP could generate an error if any = required fields are not set. $customer =3D new Customer{ name =3D> 'mike schinkel', email =3D> 'mike@newclarity.net ' } echo $customer->name; // Prints "Mike Schinkel" Actually, a required modifier would not be required to implement object = initializers (no pun intended) but it would enable IDEs, PHP and other = tools validate whether or not a required fields has been set. As an aside, it would be interesting if there were a __validate() magic = method although it might harm performance too much. OTOH, it should not = harm performance unless you actually use it: public function __validate(string $field):bool { switch ($field) { case 'name': return !empty( $this->name ) && strlen($this->name) >=3D 3; case 'email': return validate_email($this->email); } return true; } > a desire to use this language feature will drive architecture. ... = this will > provide the wrong kind of motivation to make what are, effectively, > architectural decisions. The reason I badly want object initializers is to empower architectures = that currently are not possible in PHP. So I see this as a positive.=20 I see architectures used in other languages that reduce tedium and = enhance reusability that we cannot implement in PHP because of lack of = an object initializer. But maybe I do not realize what you are seeing? Can you please = elaborate on the architectures you fear will emerge, ideally with = examples and reasons why they should be avoided? -Mike --Apple-Mail=_404C7241-22CE-4971-9EA2-C51CAEDA2E49--