Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125961 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id A368E1A00BD for ; Fri, 15 Nov 2024 07:38:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1731656492; bh=9ggYF/MPWH64bYQcBOiWBym0klpBZyoMXvsTwMf8gYU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=gsmgLheW+wLMlVbV5c2Br8wp77BXxQ5/38zPIHG6idhIgkAyKtWxTHajB25g5uq+1 Q1llCA8cSdQHDUxSgthryx+5wACcvUEnvobs+aXG1rS6L/qI+51tM5+Q/Vs5dK9Az6 i3p/KCRG8+MX7u2wYIWN7GJwm2elHAdKqA4XmpCR9GiK9w2/ZCrPlf2ogcd/zCwLP8 CNesmZfGRLzZAYGG15XeAKQfA6z2j6cgBVzYeU7yoFEu+czFRIenWdcLuDJ6r8dCur 5bugNPWhnf8QiFZhRfj2mYmWvffDao8wTRekyz504uBOy/E3Ez8z4U+F/d7uRPiWoV BhqvJouex+9NQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 82FB518003F for ; Fri, 15 Nov 2024 07:41:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: ** X-Spam-Status: No, score=2.4 required=5.0 tests=BAYES_50,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-ua1-f50.google.com (mail-ua1-f50.google.com [209.85.222.50]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 15 Nov 2024 07:41:31 +0000 (UTC) Received: by mail-ua1-f50.google.com with SMTP id a1e0cc1a2514c-851d6c397bbso621791241.2 for ; Thu, 14 Nov 2024 23:38:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731656334; x=1732261134; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=nTanzOUovLfzBoK+tkYhghVQxusRCwt1/BmsnG1ELKc=; b=iZnBne31g5OzAFYjvRsjskUyDhYbZ07gTZXlnzCNK+zJkGlqFSzOOhjW4LhiQkkXIN Yb7VMPBIVHIefmRPBn5/e0Zno5qdHvJaS/uEPOxUDtPjqb8CmEAdGaNx5PCjCll9FUI2 4KyyKB1rp+4XVH7O9IXxgE9Q30mYLDVkDfjIkryXhfLJgz5ebsswwIb03cMII7ZrB2GX +Dy3Ql5rtMo+yYOrWz5ZRrQsqddLWPd7WX9imPmIRlmz7D+e7u+AhH6UnnFwu9GMBSOG Fmw8pqHRh31QJrAN0l784qpe5+cL1xzeqUDx0vMJlUOZTlaN9emPSkzO96bvPTSqafV0 2Xiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731656334; x=1732261134; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nTanzOUovLfzBoK+tkYhghVQxusRCwt1/BmsnG1ELKc=; b=r7OOoK5StHGoyoKUmlzo3LZ3y9ArOhxZxJ0w1ImPzQ1J72/Nrj5t3ZqLoCYCR1jHy0 fuDVzHgmDYe2gAii2WCr6e9b+aYeKsJOalmes9mUySPrQX+nPHCS+UpSaKq8d1pD9+T+ BULTQZFX0rFremwOKXi8Z0gKLckLVJv7CB3A/w/hwa2lzgmfPIaKfW4KGq3kkyvEWZUz EQe3e/NQEhv1OLgNYfQkd0GFXipybjodA2BCckrEaJT8uiJqKL9D/pYqT3ud9dhAG4AV BDw+nTg89ArMQ3AwCeYxDHYH6brVj+y/lVsA1b2uFf+vtVv3K0vDsiyws4037WfS/DTY znzQ== X-Gm-Message-State: AOJu0YwFmJ0s1qnpmHuHeVlFIEbNtjThfd6TzSkV83CtuLs1w7/nzFn0 4tZGFnHOqZpXy1FPCZbsf//T73P9j60qCCUIoEm6Hiaue61os7LFmAoQ89OIeeWfjBq9pPmnOCd /oXPQoDMP+serh0SN+hHSioWN+6I= X-Google-Smtp-Source: AGHT+IHwudOU+880aRhlh5VpkmKiT4hUJQu2Ok4UH2HmfK6sqjRT7QVai8LfoURT+uDheBQGV58UP6bFopS+hmTybGg= X-Received: by 2002:a05:6102:304a:b0:4ad:4895:ce1f with SMTP id ada2fe7eead31-4ad63195954mr1833115137.17.1731656333662; Thu, 14 Nov 2024 23:38:53 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <3dcc5393-6ce7-465c-abe6-ecb687b0f369@app.fastmail.com> In-Reply-To: <3dcc5393-6ce7-465c-abe6-ecb687b0f369@app.fastmail.com> Date: Fri, 15 Nov 2024 12:38:42 +0500 Message-ID: Subject: Re: [PHP-DEV] [Discussion] Make objects unpackable by default To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: ilyaorlov124@gmail.com (MrMeshok) I absolutely agree that unpacking objects to functions is a bad idea, my use case for this is different. Take for example that you have API endpoint that returns user balance ```php readonly class Balance { public function __construct( public float $debt, public float $credit, ) {} } ``` And a service that do all the logic ```php interface UserService { public function getBalance(): Balance; } ``` But you also have endpoint for general user information that includes flattened balance (better thing would be to just have balance property, but API already designed and you can't change it) ```php readonly class Profile { public function __construct( public string $name, public float $debt, public float $credit, ) {} } ``` So a logical thing to do is just unpack (flatten) balance object ```php return new Profile( ...$service->getBalance(), name: $name, ); ``` =D1=87=D1=82, 14 =D0=BD=D0=BE=D1=8F=D0=B1. 2024=E2=80=AF=D0=B3. =D0=B2 22:2= 8, Larry Garfield : > > On Thu, Nov 14, 2024, at 7:37 AM, Christian Schneider wrote: > > Am 14.11.2024 um 10:59 schrieb Marco Pivetta : > >> On Thu, 14 Nov 2024, 11:29 MrMeshok, wrote: > >>> Hello, Internals! > >>> > >>> As you know if you try to unpack a regular object (`...$object`) you = will get an error: Only arrays and Traversables can be unpacked. > >>> I don't really see a reason for this restriction, because foreach on = objects works perfectly fine, so I made a feature request on GitHub to chan= ge this behavior (https://github.com/php/php-src/issues/16038) and was advi= sed to see opinions on this here. > >>> So, what do you think? And does this change require RFC? > >> > >> TBH, foreach on objects is already quite an abomination in itself =F0= =9F=98=AC > > > > It is a bit less weird if you use value objects or json data in object = form. > > I would be careful to judge how other people use the language, just say= ing ;-) > > > > - Chris > > Judging how one should use a language is a core component of language des= ign. > > I'm with Marco on this one, and I'd be more than happy to get rid of fore= ach($object), too. It's way, way more complicated a topic than you think. > > How does visibility play into it? > Should hooks be invoked or no? > Do references play into anything? > > Those are some of the questions we had to deal with for hooks and foreach= (). It's non-trivial. You'd need to address all the same edgy issues here= , too. > > And that's assuming it's even a good idea, which I don't think it is. If= you are using a value object to define unordered arguments, just pass the = value object itself. If the receiving function can't handle that, then it = is simple enough that you don't need a value object. If that's not the cas= e, then your code design is bad and the language should not try to paper ov= er that for you. Either simplify the function or adapt it to use a value o= bject directly. (With union types you can even do so in a BC fashion much = of the time.) > > --Larry Garfield