Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107086 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 34512 invoked from network); 14 Sep 2019 11:13:56 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 14 Sep 2019 11:13:56 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id E45CE2CE19C for ; Sat, 14 Sep 2019 01:50:17 -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=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,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-xd41.google.com (mail-io1-xd41.google.com [IPv6:2607:f8b0:4864:20::d41]) (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:50:17 -0700 (PDT) Received: by mail-io1-xd41.google.com with SMTP id n197so67956666iod.9 for ; Sat, 14 Sep 2019 01:50:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=8A2kEZItc4AYpLDB58Cl0OnTtnCIsHEKIP7oq5l7udE=; b=DbZakw8cgp57+H8SpLjFzZ5lkNOAbS4M06wWwNXOTvJPn/gWkMhe/qpWl1ZZ+WRtC2 YgqiFqS/2wgtU95c1ZbKgKiUOH6lsk/NRQZW0SORCkv95SQt4UVWuws4PXSQ3mvDItSy 8LD/RQgeWF3A2DOsw5Cm4BSTMbRzeZ+kHxwsxcxACtVOUB1tDBdnLz1Q+Ip1A9J2ZZme 1SlngiOYG1kQB0R6KTR7eE+rSmP/n6Xo2+HupsyQmSuw7P8xRJlxcxW9zW69HDtlx4Aw hoHPNblARDz/9MM5K4SD9S2OSZlgxtKB/A1BIReQ3Y0Khux6wfRcrJXV0+/GjU8JcV2G GXRA== 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=8A2kEZItc4AYpLDB58Cl0OnTtnCIsHEKIP7oq5l7udE=; b=QbuLCWRo7WyAhsfzBBOPoxVrtEFCbXX0xlvlnVxLGh8jrGcfdfMlT8ikTRlI2Ts6jM 9tBLqqYGikI9udstO7DIYr1g21Z35qRYLcih/+oPL6li/L0mcJs3uZu3dMcYjyhug18W 3pKhEshik4BP3GVvv9g1PQqjn48o7RSdSMPFF7FZsu12ViV5nNU+Bj1A3NcgWgZUqKxH QB0uFTHOi0QtYjOnyJR6XTMSdhWqmVgn1WSTjc4xbbwYKIc6e/Y/dj9Begs0f5ldxwij UxbXtTEKT4m6bvlcYW/R1hn1PNOv/IBSqK+JMRSNecC5/RGT3SVK7YsUJf/gwK/Z6UAA sSrQ== X-Gm-Message-State: APjAAAWu+K+IJZJt23WA85EkHYwydejCGn3ppT5kTUgMKYVSfw0HjkL0 iCvQR03Jp7ojbETsrMiyoPaN/LfhADZ71wunY8VhXSNv8H6Abw== X-Google-Smtp-Source: APXvYqzQWNmIaVYdTWqGnr+OyJLPvECuWgKYNePsVS9KCmDUykgWtugxCJIUYQ2N//13/0zzwXYgU1NVgMK5Hj0h+NY= X-Received: by 2002:a05:6638:778:: with SMTP id y24mr29351139jad.122.1568451016622; Sat, 14 Sep 2019 01:50:16 -0700 (PDT) MIME-Version: 1.0 References: <0275793C-EC66-4693-B74F-B794E6FF0AC6@newclarity.net> In-Reply-To: Date: Sat, 14 Sep 2019 10:50:04 +0200 Message-ID: To: Mike Schinkel Cc: Rasmus Schultz , =?UTF-8?Q?Micha=C5=82_Brzuchalski?= , PHP Internals List Content-Type: multipart/alternative; boundary="000000000000b1894105927f75ac" X-Envelope-From: Subject: Re: [PHP-DEV] [RFC] Object Initializer From: ocramius@gmail.com (Marco Pivetta) --000000000000b1894105927f75ac Content-Type: text/plain; charset="UTF-8" On Sat, Sep 14, 2019 at 10:47 AM Mike Schinkel wrote: > > On Sep 13, 2019, at 3:18 AM, Rasmus Schultz rasmus@mindplay.dk>> wrote: > > > > 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. > > 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. > > Using your Customer example, the __init() would be automatically called > after __construct(): > > class Customer > { > private string $name = ''; > required protected ?string $email = null; > > public function __construct(string $name, ?string $email = null) > { > $this->name = $name; > $this->email = $email; > } > > public function __init() > { > $this->name = ucwords($this->name); > $this->email = 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. > > 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 = new Customer{ > name => 'mike schinkel', > email => '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) >= 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. > > 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 > > Hey Mike, lazy initialisation is already possible in userland: adding more magic to the language for a use-case that is already implemented seems problematic to me. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --000000000000b1894105927f75ac--