Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107073 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 82672 invoked from network); 13 Sep 2019 12:39:26 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 13 Sep 2019 12:39:26 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id CA5FA2D2026 for ; Fri, 13 Sep 2019 03:15:33 -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-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) (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:15:32 -0700 (PDT) Received: by mail-ot1-x32e.google.com with SMTP id s28so28933169otd.4 for ; Fri, 13 Sep 2019 03:15:32 -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; bh=rdhEJ4EtH7ndfIHtWXd5fL35IY43jBmL1z133MxHil8=; b=MnCUxuu58fhQbUdJE47ffOqnQuxEgUQYyTFpHcWpNhSTKXvSKfxm0bnuACOvNYodHX njYF3zcJ07f7Jaqm88Eb6Y/g1qZT9chDO8uT2Q/3H2hx1gvxZQO61RqHvYEYbtqRnbuf zU9dk47A1iiFVfEOPadqHWv/xKWvjOGL4XduIh/1fxMvMERhbj8eHPr5zU6W+G2Zp4zp mapCZHt4IeZxakFkLN1+0xhqrgYq3wGvEuaCjqKihi6p3HEJ8PLX2nD8z0YQ8jukwIZf 91qOQ+RH2nH9jn/FZZqwrqX+nR152OXN41rGehwUrmkmyXwUGIAsLpLP39qVmH0lB3g5 PLJQ== 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; bh=rdhEJ4EtH7ndfIHtWXd5fL35IY43jBmL1z133MxHil8=; b=ncDaYa+DoAJX3/ZZrH9Ok7kQFhDfKkoBktZ+72M7vsRiV+aH8E6ExecHukILtd98RL zGOVH3pO0qLOVIdb35XZTlDN3I9lT9vFLOjHBP1p9/MyQGFJrWGXAdG2EqoLMx7XszuM aQiosy29ycDYFk8REcLht4TMV9pGiHfJo+GPJ5/SCG/Gn+T/5j9f8WzHPrkwKF+bQU9q a9Vcq8HjX6zA1MXKy+d6PFl5h3r7Lwf2qV4wqLk910DGTMUH+7OuGFQ/Ff6o3Xct9Fxb 1jl1IEBXfZOYzIMTQ5kcT/9w+SRZrduGwcQW6N6qomAgZOHoxIwaLzBi5W0Tj0VHp+Hc B5hg== X-Gm-Message-State: APjAAAUBTgi0jCpnxheuCZRGcLVzQf8n1LsczY5B6hw9AQmiUfiQa3RQ RNO92uxVDZfv280YtyJ/+fF/plyGNgTAXYWUpOzNSl38 X-Google-Smtp-Source: APXvYqxlu2W8gbGusDQoLFDJLI6yRYPWzTjNrkWVRyRxZ9i2ZRWHKxtb+d33nfZaAJZnL4S/dAMkV8vk+41//tmQyqo= X-Received: by 2002:a9d:4102:: with SMTP id o2mr41216465ote.187.1568369731192; Fri, 13 Sep 2019 03:15:31 -0700 (PDT) MIME-Version: 1.0 References: <74C03BB1-28DF-4694-A91D-2516C545F8B2@gmail.com> <3CA484B0-BCA3-4A2B-B2A6-8B845F9C462B@newclarity.net> In-Reply-To: Date: Fri, 13 Sep 2019 12:15:19 +0200 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="000000000000b44bdb05926c886f" X-Envelope-From: Subject: Fwd: [PHP-DEV] [RFC] Object Initializer From: arnold.adaniels.nl@gmail.com (Arnold Daniels) --000000000000b44bdb05926c886f Content-Type: text/plain; charset="UTF-8" > I don't like how it works for anonymous classes. It's more difficult to >> implement since the compiler doesn't know the meaning of the (first) >> bracket. It's doesn't make the code more readable, for the same reason. I >> think it's better to not support this syntax with anonymous classes. >> >> > Probably lexical scope for anon classes would be better here, but due to > fact that proposal is to > use initializer block instead of constructor arguments, that was the > reason why initializer block > got before anon class definitions. > This adds a lot of complexity to the implementation for very little benefit, as the code (as presented for anonymous classes) is really confusing. > >> The examples do not show how constructor arguments are passed. I'm >> assuming >> it's >> >> $customer = new Customer("foo") { >> name = "John" >> }; >> >> > The examples don't show that cause it's forbidden. > There is a note on that in RFC on purpose > > > Note! Object instantiation allows only constructors without required > arguments to be used. > > Any class which requires passing arguments to constructor cannot be used > in combination with object initializer. > > Using constructor arguments and object initializer would introduce noise > and you'll be potentially initializing object twice: > using object initializer block and using constructor args what may be > misleading. > I don't see any technical limitation to justify this. $user = new User($organization) { name = "John Doe", email = "john@example.com", }; Is just equivalent to $user = new User($organization); $user->name = "John Doe"; $user->email = "john@example.com"; > Setting the properties does not interfere with the constructor. This is also confirmed by > constructors are called before initialization takes apart You may use constructor argument `$organization` to setup the object. The initiate syntax isn't a replacement for the constructor, it's a replacement for setting properties after the object has been constructed. > Ok, I will let Arnold reply to you on this if he feels that your reply did > not address his concerns. > > Omitting stdClass is outside the scope of the current RFC. I'm inclined not to discuss it further or take it into consideration for this RFC. New RFC, new discussion. However, I do agree with Mike that using `=>` is more intuitive. It's not specific to array key pairs, because you only use that syntax at initialization of the array. $user = [ 'name' => "John Doe", 'email' => "john@example.com", ]; and $user = []; $user['name'] = "John Doe"; $user['email'] = "john@example.com"; The following feels similar $user = new User { name => "John Doe", email => "john@example.com", }; and $user = new User; $user->name = "John Doe"; $user->email = "john@example.com"; --000000000000b44bdb05926c886f--