Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116158 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39102 invoked from network); 25 Sep 2021 15:03:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Sep 2021 15:03:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 42E92180538 for ; Sat, 25 Sep 2021 08:45:18 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_HOTMAIL_RCVD2, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8075 40.80.0.0/12 X-Spam-Virus: No X-Envelope-From: Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10olkn2047.outbound.protection.outlook.com [40.92.40.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 25 Sep 2021 08:45:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=momIUQ8Hy4PgH0uWZ6Av7V+VnUIpdVbLlGZWRdhkTo7hF8PPDL+krfowirKV5W08lJRvI0zF72xJwI9xScWzlg4jesv9BtxhndPTnHMeWk8SWXz4PSfzpppNN+j7eimATMiaiWuwlVPHFzkJFRnGSrFMXeoFiHaRwJyMmbeehPK85G5lK42vx5iFTTsesem0qGLg7T/slMRDLEimtTFSOsk/L0UBdW8UodjNhBIa550aNRkjigO5reULgz1rlDFcpx3YzPMsW92RXpr551h5+kuE5diq505x9gQAVJVxLh29HVSFunKOqjAbvEry1tpy0xQSr91w5ymYjM3GXXn6Xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=6+Y5yfhj3t6ZPhfC65a5xQZvzY+oGBufFXPRHlcsUYA=; b=YtbsAK4sjVPWkL4LHf+SYmPtJxVzb2hMfksvHyswNobllc9DNvxaliLpHBC3VigqT2jPMHDek0QzB/dUubZ+r8X3a3Cuann2HbwdOhnZ7zXJTETVt1exj+BloGzfiU+sy02RR0XTT51lAh9PJXi+HyYE/7DOcglTDQjXrFkDWZGrhyBEZjMWKYqwzp5rVWVkPTbk73ZL0qq1s2MKrSPncsRPQ4cM852SS2LSQx/ovZdwHf3jcHVWby5MscE8mh7XDkRXHKbV/Nu+CMkpsyRINFsgRuGbkmMCu3QjdbPfREhYNVdscxLO1v/jsI63ij5tGjFTQR64Vra3LVRi8WV6bg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6+Y5yfhj3t6ZPhfC65a5xQZvzY+oGBufFXPRHlcsUYA=; b=o+yWVcUSiKXPyO17I6AeolWGHmlQ8Uzu0xi58MTtUBHMOPl1btowjVRm80WX756lQktCT1Ri4xrPti7d6lugoYihHRszbXGySx9WdDjgfez5BOByyK4A61GwEBBy2FRoS9wcaBn4uNoQ66pKs6Rsvov3zude/LjU7RLivcsVRTP4ld0pKMBcAy1i+2FWAPO/mf9/pY2RD7lrFeHX9MDjbY5POoJ91CbfqN05lzTlWApionDIEdCnHzdKUPaLwKF4bdCls2P4KhvyNnaHVOwfWSHYLdeVJox2gcOMt8EWm5IxtGPGu16mbo5MYb1+3O2eFBSJjuh7X5HQ4d0lWKyiRw== Received: from DM5PR17MB1481.namprd17.prod.outlook.com (2603:10b6:3:147::17) by DM6PR17MB2796.namprd17.prod.outlook.com (2603:10b6:5:12d::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.14; Sat, 25 Sep 2021 15:45:16 +0000 Received: from DM5PR17MB1481.namprd17.prod.outlook.com ([fe80::e832:bd41:4114:64d2]) by DM5PR17MB1481.namprd17.prod.outlook.com ([fe80::e832:bd41:4114:64d2%11]) with mapi id 15.20.4544.020; Sat, 25 Sep 2021 15:45:16 +0000 To: "internals@lists.php.net" Thread-Topic: Allowing `(object)['key' => 'value']` in initializers? Thread-Index: AQHXsiErO0Vz7k4CHEy3AX0ZJcoPiw== Date: Sat, 25 Sep 2021 15:45:16 +0000 Message-ID: Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: suggested_attachment_session_id: 7b7bf68f-d365-7f28-3910-fc94c21b9e1f x-tmn: [97zF6N1LCI2zl7OBA5FA9ZXV4emepNkbnh8etVxaaQH0HFpcMdnVbUyGYtIwpNpsR/ZCWMIWR64=] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 9bd6389a-21b6-4d66-e8cf-08d9803b789c x-ms-traffictypediagnostic: DM6PR17MB2796: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: L0cx4+uQKxQtZEJ7mjJ3Rwyntq2jGgm9iLLH0h3bmvYl4mrUCP+PQ7+tPHTYfYp6p7SrfG7BIrIwnz+b7e7dp38wHNTAv5YOQIiKTGQCgt4h22VCmdN86zx3UCkpt/fOSa59x+38RccagTWB9rX8N0p2Lgyq/Pf5Vi7dKUFQW0IzDrrOlka8ZijJKFcUZ40Z9xEczjidaQ6qriyhfkPgeEn4DYGWH6thELAL1b09u7rtHNoIcbJglJxPZ4oy3VnmvuFg0wBZsHXMIMTeIswZAeikWoMlhZkzR70PPl0z7HPEpGRXqJBADD/gQG17hqM4iFTIdNWMi/5QuUrzpeow2tHeYeMO8Xe6/8o/U/pfq0rVnewWLyiBPlCq3SM/2yaA4tZmYTTxzugy3U8tDL4uv55+pQHOEEMhETmBPl4rmoyWWzos1wV/P+AssX7FLglVlvy9P4M/FB4k9DMibESjmQM0FEMzjs2QAiGov+/JspBw/QCmQ4sQF+aU1wX9zmD33qhbPAaQfd8gqNFEzNHITg== x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: B93hM+SvUA1JpnVOsGJ2irbFUYZqpOS1tWLw5cQeWuusDa1ZMbMH7wOfhard+t6zHn/xAcig/qxDumTepCVb9Cciv8vGW35CsjGOpelOxwunjnnnjzph3nRBmg329tf8eCrUASmWlXw4PffiqoqEupAEe3EZDcWpZVhJyEMTS0LF/buJEdCUytEkeMsWZhiby+sQ/ji/85OTBUVmNEYhEg== x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-3174-20-msonline-outlook-21df5.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: DM5PR17MB1481.namprd17.prod.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 9bd6389a-21b6-4d66-e8cf-08d9803b789c X-MS-Exchange-CrossTenant-originalarrivaltime: 25 Sep 2021 15:45:16.3641 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR17MB2796 Subject: Allowing `(object)['key' => 'value']` in initializers? From: tysonandre775@hotmail.com (tyson andre) Hi internals,=0A= =0A= In PHP 8.1, it is possible to allow constructing any class name in an initi= alizer, after the approval of https://wiki.php.net/rfc/new_in_initializers= =0A= =0A= ```=0A= php > static $x1 =3D new ArrayObject(['key' =3D> 'value']);=0A= php > static $x2 =3D new stdClass();=0A= php > static $x3 =3D (object)['key' =3D> 'value'];=0A= =0A= Fatal error: Constant expression contains invalid operations in php shell c= ode on line 1=0A= ```=0A= =0A= What are your thoughts on allowing the `(object)` cast in initializer types= where `new` was already allowed, but only when followed by an array litera= l node. (e.g. continue to forbid `(object)SOME_CONSTANT`) (see https://wiki= .php.net/rfc/new_in_initializers)=0A= =0A= stdClass has never implemented a factory method such as `__set_state` (whic= h is not yet allowed). Instead, `(object)[]` or the `(object)array()` short= hand is typically used when a generic object literal is needed. This is als= o how php represents objects in var_export.=0A= =0A= ```=0A= php > var_export(new stdClass());=0A= (object) array(=0A= )=0A= ```=0A= =0A= Reasons:=0A= - The ability to construct empty stdClass instances but not non-empty ones = is something users would find surprising,=0A= and a lack of support for `(object)[]` be even more inconsistent if facto= ry methods were allowed in the future.=0A= - stdClass is useful for some developers, e.g. in unit tests, when using li= braries requiring it for parameters,=0A= when you need to ensure data is encoded as a JSON `{}` rather than `[]`, = etc.=0A= - It would help developers write a clearer api contract for methods,=0A= e.g. `function setData(stdClass $default =3D (object)['key' =3D> 'value']= )`=0A= is clearer than `function setData(?stdClass $default =3D null) { $default= ??=3D (object)['key' =3D> 'value']; ` =0A= - stdClass may be the only efficient built-in way to represent objects with= arbitrary names if RFCs such as https://externals.io/message/115800 =0A= passed=0A= =0A= Thanks,=0A= Tyson=