Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123535 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 D6B6B1A009C for ; Fri, 7 Jun 2024 09:07:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1717751306; bh=dCRvbbFpF6uA4r9Whdc+bwQB9Dw7Gm9VccB57gVFwxc=; h=From:Subject:Date:To:From; b=JudvX0y/Lt/9zC/y/3beGwIfkro6cK1n3i2GFuOeUZoEBLRUZ5/vRnPB+sn+VWh0b JYTF5KZtzS64r0tmoLcFJKbWKKuEOTr6dqccMUFSOUInCbGPscViTFt+jRqFBg4pPn BKb0Nql7fGRAyeC3g5LghmW8woZdCH7/0kbNyebLNTi+KX6DPkaD31alTx9Ux2eJmC tFbHIIGjXAmRpuxX6Olly0BUlb98npqzghD5vQirRXP93X+3DJ5cbUpXOPqwV/+j7o IHfgpYBW6MbnSf2zYf5sj5JGYeZ9ogbXaQcquF8jSjYSxC/dNFZvdHLRx3TISUKm3s S8spoYV9k+Ynw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D785818004B for ; Fri, 7 Jun 2024 09:08:24 +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=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.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, 7 Jun 2024 09:08:24 +0000 (UTC) Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-a6ef46d25efso7068266b.0 for ; Fri, 07 Jun 2024 02:07:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717751237; x=1718356037; darn=lists.php.net; h=to:date:message-id:subject:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=cr+Nw2FTgpz4Bd4aTaTcMMkWHwPjr9r++bUDbOmDM4Q=; b=QW9ftfdKFB5wf/7lL1/P9A/IpEsAltJpBaYdUz4eJMUbQYsszvO7kjS24KO+5CBAO7 yiPa/Y0SVs/lankhPREYYQDgrZO/0yDN/tF2b5Rln1VC/Qj+VYlVme9nOkrX1KuowdTs Mjq0svDEzpxWCe3ZpeUddBFGBsGYuYDUa9/eyKlhZIgFUKoMHQsTB0DoJYjtxRLFKqkM 68OYx/zl1BG5QZgg9eM0Wq2Vh+My8hIkaACJKbhIUtsRiAKiz3sBbXHbSt7SPsBhqGr4 OXl/IOXaF4xb4BeYx7Nl85sVGlyDMjWK95E5H982HEnBp0NdqG+huYqOFUI+HzJYk9hC CRhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717751237; x=1718356037; h=to:date:message-id:subject:mime-version:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=cr+Nw2FTgpz4Bd4aTaTcMMkWHwPjr9r++bUDbOmDM4Q=; b=lMQYM9rotuhapeSAktqTZ6iwl2fkq8lEiw/BiMgQLSI3LmL7D4nIW9uGxmA5t+O6/p xG32Zu1GPEAvrOyK69X5Kt6D0G8jtWYy35fXvmTQfdGzuOWHnE0B9kLb0yHdXDIYi4v+ t3pNpz3bY4AyxePKQH3AINRR3bBWgU11dyFTej0l0Z0E3ic3GFYMOKFOBNB7OzvNV1s/ zILW0Zew9A3HzeTeGEFsve3yEFo4Op4pbYePHBbNlY0729d34YhXoEyEdmtK8MrRg4Cw yC3TofFazM7MeDLYoG4pZuMWempp+3YYJ+GyuCj5t//QsqkqQe4nergvJxUuCJDwX9r6 DMKQ== X-Gm-Message-State: AOJu0Yw1CX2XyEZ8rrfUIrwj6+UGKE7bric6BXo/+mpwzKsXiESzRJt8 gIby1gfCOO4d3zfctu08wdT3zyXaoMuizFSV1dUfG4wUDjjTFXSfgn4+qg== X-Google-Smtp-Source: AGHT+IGosTzA5qtDICGd1EvNatspIxKsM0ZeY9o7OCRwQqUXUNJLlgXinnmQhVI3PrCpOU1r5bG8Ug== X-Received: by 2002:a17:906:1406:b0:a62:4b4d:5447 with SMTP id a640c23a62f3a-a6cdaa0f849mr143085766b.62.1717751236859; Fri, 07 Jun 2024 02:07:16 -0700 (PDT) Received: from smtpclient.apple ([89.249.45.14]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6c8070dbd5sm217892366b.178.2024.06.07.02.07.15 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Jun 2024 02:07:16 -0700 (PDT) Content-Type: multipart/alternative; boundary="Apple-Mail=_93CAE697-BF3F-42B4-8131-085F211A93D8" Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.600.62\)) Subject: [PHP-DEV] Default value for readonly properties Message-ID: Date: Fri, 7 Jun 2024 11:07:04 +0200 To: php internals X-Mailer: Apple Mail (2.3774.600.62) From: claude.pache@gmail.com (Claude Pache) --Apple-Mail=_93CAE697-BF3F-42B4-8131-085F211A93D8 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi, As of today, readonly properties cannot have a default value: ```php class UltimateQuestion { readonly int $answer =3D 42; // Fatal error: Readonly property = Question::$answer cannot have default value } ``` The rationale given in the original RFC = (https://wiki.php.net/rfc/readonly_properties_v2) was: As the default value counts as an initializing assignment, a readonly = property with a default value is essentially the same as a constant, and = thus not particularly useful. The notion could become more useful in the = future, if new expressions are allowed as property default values. At = the same time, depending on how exactly property initialization would = work in that case, having a default value on a readonly property could = preclude userland serialization libraries from working, as they would = not be able to replace the default-constructed object. Whether or not = this is a concern depends on whether the property is initialized at time = of object creation, or as an implicit part of the constructor (or = similar). As these are open questions, the conservative choice is to = forbid default values until these questions are resolved. A new fact is that properties will be able to be declared in interface = since PHP 8.4 (as introduced in the recently accepted property hooks = RFC). It is true that, for an individual class, a readonly property is = functionally equivalent to a constant. But my class implements an = interface (or, before 8.4, follows a non-written protocol), and whether = that particular implementation choose to always hold the same value for = a specific property is irrelevant. ```php interface Question { public int $answer { get; } } class UltimateQuestion implements Question { readonly int $answer =3D 42; } ``` Therefore I think it is reasonable for readonly properties to accept = default values. About the concern given in the original RFC: =E2=80=9Chaving a default = value on a readonly property could preclude userland serialization = libraries from working, as they would not be able to replace the = default-constructed object=E2=80=9D... TBH, as I almost never serialise = my objects, and even less often customise serialisation, I can only = guess what the effective issue is. My best guess is that a custom = `__unserialize()` method would not be able to reinstate the incriminated = readonly properties, because those would already have been initialised = with the default value. If this is the case, a solution is to do for = `__uninitialize()` the same thing that we have done for `__clone()`, = see: = https://wiki.php.net/rfc/readonly_amendments#proposal_2readonly_properties= _can_be_reinitialized_during_cloning =E2=80=94Claude= --Apple-Mail=_93CAE697-BF3F-42B4-8131-085F211A93D8 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Hi,

As of today, readonly = properties cannot have a default = value:

```php
class = UltimateQuestion {
    readonly int $answer =3D 42; = // Fatal error: Readonly property Question::$answer cannot have = default = value
}
```

The = rationale given in the original RFC (https://wiki.php.= net/rfc/readonly_properties_v2) was:

As the = default value counts as an initializing assignment, a readonly property = with a default value is essentially the same as a constant, and thus not = particularly useful. The notion could become more useful in the future, = if new expressions are allowed as property default values. At the same = time, depending on how exactly property initialization would work in = that case, having a default value on a readonly property could preclude = userland serialization libraries from working, as they would not be able = to replace the default-constructed object. Whether or not this is a = concern depends on whether the property is initialized at time of object = creation, or as an implicit part of the constructor (or similar). As = these are open questions, the conservative choice is to forbid default = values until these questions are = resolved.

A new fact is that properties = will be able to be declared in interface since PHP 8.4 (as introduced in = the recently accepted property hooks RFC). It is true that, for an = individual class, a readonly property is functionally equivalent to a = constant. But my class implements an interface (or, before 8.4, follows = a non-written protocol), and whether that particular implementation = choose to always hold the same value for a specific property is = irrelevant.


```php
interf= ace Question {
public int $answer { get; = }
}
class UltimateQuestion implements Question = {
    readonly int $answer =3D = 42;
}
```

Therefo= re I think it is reasonable for readonly properties to accept default = values.

About the concern given in the original = RFC: =E2=80=9Chaving a default value on a readonly property could = preclude userland serialization libraries from working, as they would = not be able to replace the default-constructed object=E2=80=9D... TBH, = as I almost never serialise my objects, and even less often customise = serialisation, I can only guess what the effective issue is. My best = guess is that a custom `__unserialize()` method would not be able to = reinstate the incriminated readonly properties, because those would = already have been initialised with the default value. If this is the = case, a solution is to do for `__uninitialize()` the same thing that we = have done for `__clone()`, see: https://wiki.php.net/rfc/rea= donly_amendments#proposal_2readonly_properties_can_be_reinitialized_during= _cloning

=E2=80=94Claude
= --Apple-Mail=_93CAE697-BF3F-42B4-8131-085F211A93D8--