Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107046 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 15180 invoked from network); 13 Sep 2019 09:46:35 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 13 Sep 2019 09:46:35 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 56CEB2D1FF1 for ; Fri, 13 Sep 2019 00:22:41 -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-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) (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 00:22:40 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id 205so17539584pfw.2 for ; Fri, 13 Sep 2019 00:22:40 -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=jCjDwoHFEc4Zm8yYEIGLTjnsNUVteZrxS2suxw6NH1A=; b=eVEX1IeClnirT9tYFSa9qfrocrdThbtGCRw+dfxD7tQ4PP6w0ORJyyMKAsabpp9j3o XX6pLBKRL7nDxt2NQQOv2nOVECtDOtSR3kqewWQ9OeoWME4nay+f5JlT/yqNaQi2YGjr 6tbKhSSiaFUIpmLjz0bnu+PZt9ljBh0GQtaWr2h45b9bjhbdD784cTAxKwQ/I7abKmlG Pdq4PW63I8G7NAB3AhhKJyTOEtsrzRM5eXM1cPGXpKt4bnE5IrBgDbeE4UulPZtmKNnI c6XbT7LK7gWBSaf0zIyuGCS1gb8HuZEHJNVZhTQ4agVvrMiPmJXdIwKqKjAQNlyBYpS5 mrvQ== 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=jCjDwoHFEc4Zm8yYEIGLTjnsNUVteZrxS2suxw6NH1A=; b=FX5n0uC0rHYf/BxHoVrxWYP1f6w2qnYkDKEwKqjvDoV9mvFY0Y2uk0bUZt/VYcN0Eh jteXN1XUE/SSjJOKdKDi1UZmz3zpr5Jb0K2On73dkIUYFD9ch+DyvYH6R4vAJzQ/aW2W WGMSv9x44OB+C6xjehY282La1rKX+TEf0G9Q4JIKuiqg4kEgGazce/MIxk/zI0H0KeYy /bBgzivSHPHJAoIf3lplDxdPsHVwGAG8hrsuiaKGsTwbAF6ScOVZGdGu6Zt2jUqpiukS jOErwBba6kD+c0ykSSlPEesUkUoYVLMwNdiwZ/3yBHnjxA2v7/SxNN5GmE4zAgKje94s Ua8g== X-Gm-Message-State: APjAAAX5M/aP7ZbH5mDlGi17eG8kuuiSXErchAOrTTBps0eXIYOXfDX6 NrgUXqER64y0G/WywbOo5dgszQ== X-Google-Smtp-Source: APXvYqwf7ZmA9Ot2n6Lx/RV+geqlahq2MiveBuuWmFec0tTkwHWE1oEnj5zhoVbe5eaJKW+zCjB6cA== X-Received: by 2002:a63:de4f:: with SMTP id y15mr43352789pgi.239.1568359359912; Fri, 13 Sep 2019 00:22:39 -0700 (PDT) Received: from [192.168.202.38] (50-252-183-45-static.hfc.comcastbusiness.net. [50.252.183.45]) by smtp.gmail.com with ESMTPSA id y7sm27683127pfn.131.2019.09.13.00.22.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Sep 2019 00:22:39 -0700 (PDT) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_B5B02C4A-111E-485F-90EE-83DB5B31C769" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Date: Fri, 13 Sep 2019 00:22:36 -0700 In-Reply-To: Cc: =?utf-8?Q?Micha=C5=82_Brzuchalski?= , PHP Internals List To: Lynn References: 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=_B5B02C4A-111E-485F-90EE-83DB5B31C769 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > What's the added benefit of this compared to implementing a = constructor? Michal my have a different answer but my answer are the following two = benefits plus a 3rd related benefit: 1. The ability to add strictness to object initialization vs. using = arrays of properties. PHP itself could flag misspelled or missing = properties with a warning or an error which is something PHP cannot = really do given that array keys are strings and not identifiers. 2. The ability for tools like PhpStorm, PHStan and others to more easily = identify and flag these errors. It is hard (impossible?) for a tool to = validate array element keys because there is no way to declare named and = typed array elements keys in PHP. 3. It would empowering developers to pass optional elements to functions = and methods that can also be type checked. The 2nd example would allow = type checking but the first would not: EXAMPLE 1 function foo( int $id, array $args ) { ... } foo( 1, array( "bar" =3D> "abc", "baz" =3D> 123, } EXAMPLE 2 class FooArgs { public string $bar public int $baz } function foo( int $id, FooArgs $args ) { ... } foo( 1, FooArgs{ bar =3D "abc", baz =3D 123, } -Mike > The part I like is that this can be used to replace = stdClass/structured > arrays. Perhaps something like this would nice to have in PHP: >=20 > ``` > $people =3D []; >=20 > foreach ($peopleFromDatabase as [$id, $username, $name]) { > $people[] =3D { > Uuid id =3D> $id, > string username =3D> $username, > string name =3D> $name, > }; > // and possible automatic assignment: > $people[] =3D {Uuid $id, string $username, string $name}; > } > ``` >=20 > Regards, > Lynn van der Berg --Apple-Mail=_B5B02C4A-111E-485F-90EE-83DB5B31C769--