Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107188 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 94276 invoked from network); 17 Sep 2019 13:21:03 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 17 Sep 2019 13:21:03 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id AD7652D02E4 for ; Tue, 17 Sep 2019 03:58:10 -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,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,URIBL_BLOCKED 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-qt1-x832.google.com (mail-qt1-x832.google.com [IPv6:2607:f8b0:4864:20::832]) (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 ; Tue, 17 Sep 2019 03:58:10 -0700 (PDT) Received: by mail-qt1-x832.google.com with SMTP id c3so3756296qtv.10 for ; Tue, 17 Sep 2019 03:58:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=WAJvwRn9T1M/eONZtP92Dn64PHuz5ka9/XOlCXzqNBg=; b=zkwwFJgLjwFahRlxwheSHrcYFfdvK6adG6f1t1pDoGzwuPT6kgcxyudoUe83UwU2Yi Mdt+ZUYqVtDHU1gGuSV91tHq4gnT3VU35i6hXsHKs73jmQVZ/aowldrcV43RkgJm30sY 3+87HujPLzvSUoH4ZC/8c9dq0sjLjs23lPnSLClvt0SIH1jNTOKLqbt0c2Ah4kNuvE4B gKTKv/6ykTp3E+GEDVtElyLKJz/1LSnze7wWJJpLQS5BGutIz1v9CZhMWcn0GYRDLXhw 3J7ObLMgOC40PBFSpkkSesAgXAYoXTE+w78/WiOhezqI5qpX8e7A8LLsXzsrSGFH4rTc A7uw== 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:content-transfer-encoding; bh=WAJvwRn9T1M/eONZtP92Dn64PHuz5ka9/XOlCXzqNBg=; b=WHv2Hrf4C6pZJQj0QYJOW55S3nK1Pvr6Qx8Ohg5tpw/9X4zEeBiAfl5/23X3RbASNI 0FO8nrPCDZW/U8+OSh98croV5+ZYYDw+k/iTkhwJMdgre+X5Za9UZt8qtTIJfO++uSwV 7uIFXKT/w9arIzXY3kSGBOMXQOvQFHycwz7hBAGChTvjtLDjGWWkZMxBHR+mKy2LpdV/ cj0Fbiizx0y0K2ySiubgHpAA+w0utMXe5y4IH5RN6HWJtxgwIlPyCLIggwnL3oOoDyfM 0AlUwO1KhNBRVNDb3/89wrckv2oKNAL4MmAcr89DfwNEibpskxSO9N7a5bUmsqYifqcp vZBg== X-Gm-Message-State: APjAAAW1Ua6lgfekE9bn3ZtLf8mt4fwz2Vzo/PUFEtTT2qJkpfezdylX wKjc3NRrJNGm0KH94jmKujWUlAWPr5g= X-Google-Smtp-Source: APXvYqz65p3yFj/bzo6EvPIx37FHjkvdLftSG/HYSjoqve3M12Z6lXgC9qDvooFC7jWAI1+84MX1MQ== X-Received: by 2002:a05:6214:185:: with SMTP id q5mr2399513qvr.192.1568717889327; Tue, 17 Sep 2019 03:58:09 -0700 (PDT) Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com. [209.85.160.182]) by smtp.googlemail.com with ESMTPSA id t64sm856766qkc.70.2019.09.17.03.58.08 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 Sep 2019 03:58:08 -0700 (PDT) Received: by mail-qt1-f182.google.com with SMTP id m15so3823373qtq.2 for ; Tue, 17 Sep 2019 03:58:08 -0700 (PDT) X-Received: by 2002:ac8:2ae9:: with SMTP id c38mr2908744qta.311.1568717888407; Tue, 17 Sep 2019 03:58:08 -0700 (PDT) MIME-Version: 1.0 References: <472D3E22-18D9-4C25-8961-C1DDEF482981@newclarity.net> <58673C6E-6632-42D9-BCBF-0294A62EB001@newclarity.net> <83819928-59BF-4C02-9F97-59306EE97F90@newclarity.net> <425cf402-370f-4d6f-9d9e-82a044b70c45@www.fastmail.com> In-Reply-To: <425cf402-370f-4d6f-9d9e-82a044b70c45@www.fastmail.com> Date: Tue, 17 Sep 2019 12:57:56 +0200 X-Gmail-Original-Message-ID: Message-ID: To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Envelope-From: Subject: Re: [PHP-DEV] Features related to Object Initializers From: andreas@dqxtech.net (Andreas Hennings) On Tue, 17 Sep 2019 at 00:21, Larry Garfield wrote= : > [..] > I think everyone's in agreement about: > > 1) Making objects easier to work with. > 2) Devs should use objects more. > > (Side note: I have an article submitted to php[architect] entitled "Never= * use arrays" that goes into this topic in much more detail. I am 100% on = board with getting developers using objects more and arrays less.) > > However, why do we want devs to use objects more? If we just want the ar= row-like syntax, then this works today: > > $val =3D (object)[ > 'foo' =3D> 'bar', > 'baz' =3D> 5, > ]; > > And now developers are using an anonymous "object". However, that offers= basically no meaningful improvement except funnier passing semantics. I would argue they can even be inferior to arrays. Arrays have a big advantage over anonymous object, or even over any mutable object: They can be passed "by value", which protects them against pollution / poisoning. Objects are always passed as an instance pointer (I am not going to say by reference here), which means they can always be modified in the called function, unless they have been actively cloned beforehand. Even classes that are designed to be immutable can still be poisoned by setting anonymous properties. Perhaps a future RFC could suggest a syntax to prevent that from happening = :) There are some other advantages of arrays, but they depend a lot on the use case: - Array syntax is more smooth for some use cases. - Arrays can be counted, and compared vs the empty array. - A type hint "array" excludes anything else. A type hint "\stdClass" still allows any subclass of \stdClass, which could have its own methods. Otherwise, I agree: Objects with defined classes have many advantages vs arrays or anonymous objects. Depends on the use case of course. Arrays are still good for mapping arbitrary keys to arbitrary values. -- Andreas > > As I see it, the advantages of using objects over arrays are two fold: > > 1) The PHP engine is able to make dramatic optimizations when working wit= h *defined* classes with *defined* properties. I think (although have not = verified) that anonymous classes get the same benefit but iff their propert= ies are explicitly defined like any other class. > > 2) They're more self-documenting and statically analyzable (by your IDE o= r any other tool)... but that's true only if you have an explicitly defined= class! > > So I see really no advantage whatsoever in replacing this: > > function doSomething(int $a, array $options =3D []) { ... } > > with this: > > function doSomething(int $a, object $options =3D {}) { ... } > > It's only an advantage if I do this: > > function doSomething(int $a, SomethingOptions $options =3D null) { ... } > > Doing that has many advantages, I think we all agree. But going halfway = doesn't give us any benefits other than swapping [' '] for ->. > > What we want to do is make it easier to create $options inline in a self-= documenting way. I'm all for that. But that first requires the developer = still define SomethingOptions somewhere. > > So for me, the entire "easier to use anonymous one-off classes" argument = falls apart, because there's no material benefit there other than saying "b= ut now it's using objects". For that, as noted, we already have an ample s= yntax that accomplishes just as much. > > So rather than making it easier for people to make "anonymous structs tha= t use object syntax", we should be making it easier for people to define fo= r-realsies named classes and using them, because *that* is where the benefi= t comes from. > > And for that part of the problem (making named struct-like classes easier= to work with), as noted in the other thread I find the named parameters/au= to-promotion combination to solve the problem just as well, and then some. > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >