Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107087 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 36241 invoked from network); 14 Sep 2019 11:16:14 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 14 Sep 2019 11:16:14 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 703982D19AE for ; Sat, 14 Sep 2019 01:52:35 -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-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) (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:52:34 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id a2so538813pfo.10 for ; Sat, 14 Sep 2019 01:52:34 -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=Gs1qzGuB+rbyAahtHOyz87fd/PqaARomfTWHYTPyjJ8=; b=DB3johlz+dlHVJ2Hkw2kqEmkYwUitBTfkUacb9nrNGKWtkUNn9V0Z9hn8NtfPtgoqZ pB6A/mw+jIDd61SIKVd99Tgo1cbaAS+siI5Nujz6chDWZnZzZGSVX7vY49Gmqn8LfJgQ Bqe1uSqEqM8FLc7adXFPBTlQhAvmNvgZPCF4dCSJyOHcVwChArWNaPHSBiOWARqyFIYd J7lAcK5cJi80hEh/VPud0qWJv5sqbvud6ddQmVAi7qNETumFNmKPNbs3KU3jd7zTQ2FT IYNbuGRqwktdUZpm/MUBH4lEuME42gskhpMSJBOWqvrrW+m8s8F36RzRKMiDYZF/3JEJ Cblg== 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=Gs1qzGuB+rbyAahtHOyz87fd/PqaARomfTWHYTPyjJ8=; b=Ngo+UazbkFbd6oEofLacZLAS80j7OWQZmTwQDh1agEFXOPRbQwpLMPM4pJ4ASF4hyE u10+0NV9L5t/nB9MjWxpK5hV+L5kl+7tlVfkIhy/bVHvohJEJY6nZWbLq+J5ob2L36Gh TQ3qxHRMaKes373ouxzryKUgfXW8sgnqKEDlVP+363wVyZk8DNy8vspST49sbnLzbAXZ dwAGx07pHn+sJBKBaVWLNjM5Fqj6Y8gdspXEnBDCQhFQNXFj8Lb9APAOGPCHGANVKqH8 6kf+1H3H7OL/hL+haET+Iv4fRVgd+mmNJVlE7gyQJxvXMKqrmPc50+R33tnNNgNNRNYE W08Q== X-Gm-Message-State: APjAAAXXAMG1uxmck/6/r/u4WSEmGFy7HRWIRL6tm7+8c1kC2QeISTVW oGkXMWARY9OftcOxtvq4HO15wYVs9j4sVw== X-Google-Smtp-Source: APXvYqxTe8e126UI+UoZ8lyrNb8Mjg1QIPu3qs7bbzRmSvTgGe5HOp3psEsfC+k+nGDLx842VgkWTg== X-Received: by 2002:aa7:9386:: with SMTP id t6mr62026305pfe.214.1568451153999; Sat, 14 Sep 2019 01:52:33 -0700 (PDT) Received: from [10.20.3.119] ([198.233.189.11]) by smtp.gmail.com with ESMTPSA id b3sm50290394pfp.65.2019.09.14.01.52.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 14 Sep 2019 01:52:33 -0700 (PDT) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_7D480067-0A93-4188-A060-933428DF98ED" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Date: Sat, 14 Sep 2019 01:52:32 -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=_7D480067-0A93-4188-A060-933428DF98ED Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii (Sorry for sending three replies to one message, but the list server = said my reply was too long to send as just one.=20 I could have made it shorter but then I would have had to omit all the = example code.) @rasmus: HOWEVER, I see no reason we must pit object initializers and named = parameters against each other as competing features.=20 I think there is benefit to both, and ideally could actually be two = aspects of the same new feature.=20 Let me explain using one of your examples. $car =3D new Car({ yearOfProduction =3D> 1975, vin =3D> "12345678"}); Conceptually let me ask, How is { yearOfProduction =3D 1975, vin =3D = "12345678"} really any different from an instance of an anonymous class = with the properties yearOfProduction and vin?=20 =46rom the recognition that named parameters that are collected as a = group with braces ({}) are conceptually identical to anonymous class = instances lets us to the insight we could use something like = func_get_args(ARGS_OBJECT) to allow us to capture the grouped parameters = as an instance of an object of anonymous class containing properties for = each named parameter? public function __construct(int $yearOfProduction, string $vin) { $args =3D func_get_args(ARGS_OBJECT); if ($args->yearOfProduction < 1900 || $args->yearOfProduction > = date("Y")) { $msg =3D "year of production out of range:{$yearOfProduction}"; throw new InvalidArgumentException($msg); } $this->yearOfProduction =3D $args->yearOfProduction; $this->vin =3D $args->vin; } Now assume we later realize we would be better passing in an instance = instead of hardcoding the properties? We could change the signature, = leaving all the rest of the code the same: public function __construct(Customer $args) { if ($args->yearOfProduction < 1900 || $args->yearOfProduction > = date("Y")) { $msg =3D "year of production out of range:{$yearOfProduction}"; throw new InvalidArgumentException($msg); } $this->yearOfProduction =3D $args->yearOfProduction; $this->vin =3D $args->vin; } This is a critical capability in my book; the ability to refactor code = to a more enlightened architecture without breaking it: Of course if someone were to have used ordinal parameters when calling = that constructor we could match their parameters to the properties of = Customer in order of their declaration.=20 So the following maps 2019 to $yearOfProduction and "abc123xyz789" to = $vin: class Car=20 { public int $yearOfProduction; public string $vin; ... } $car =3D new Car(2019,"abc123xyz789");=20 The above could also now throw a warning so developers can find and = replace all of those calls with calls that use the actual named = parameters, but being a warning the code would still working until they = update the calls. > 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. I completely agree with this statement, outside the context of your = prior argument against object initializers. Hopefully however, I have made the case that object initializers = combined with named parameters would address a much broader range of = use-cases than either of them would on their own? -Mike --Apple-Mail=_7D480067-0A93-4188-A060-933428DF98ED--