Re: [PHP-DEV] Re: 8.1 / Exception / Property Type / Backwards compatbility
Nikita Popov wrote on 8/27/21 10:40: >> If we don't want downstream users to redeclare the properties, why are >> they protected? >> > > To allow downstreams users to write $this->line = 123. > Got it. When I asked the question, I was thinking about passing it through the constructor, forgetting that line and file cannot be passed through the constructor. :-) Cheers, Ben signature.asc Description: OpenPGP digital signature
Re: [PHP-DEV] Re: 8.1 / Exception / Property Type / Backwards compatbility
On Fri, Aug 27, 2021 at 5:35 PM Ben Ramsey wrote: > Nikita Popov wrote on 8/26/21 14:57: > > On Thu, Aug 26, 2021 at 8:34 PM Ben Ramsey wrote: > > > >> Nikita Popov wrote on 8/26/21 09:57: > >>> Right. I at least do not plan to address this issue. If you take a > >>> protected property and publicly re-export it, then any compatibility > >> issues > >>> are on you. > >> > >> This does not appear to affect only cases where one is re-exporting a > >> protected property as public. > >> > >> Exception protected properties without type hints: > >> > >> * PHP <= 8.0 - https://3v4l.org/GWmrk > >> * PHP 8.1 - https://3v4l.org/GWmrk/rfc > >> > >> > >> Exception protected properties with type hints: > >> > >> * PHP <= 8.0 - https://3v4l.org/UX1Pa > >> * PHP 8.1 - https://3v4l.org/UX1Pa/rfc > >> > > > > These are not really meaningful examples, because you could simply not > > redeclare the property at all and assign it in the constructor instead > (or > > preferably, let the parent constructor assign it). That's the normal way > to > > extend exceptions. What made the original case interesting is that the > > property redeclaration isn't immediately redundant there, because it > > changes visibility. > > If we don't want downstream users to redeclare the properties, why are > they protected? > To allow downstreams users to write $this->line = 123. Regards, Nikita
Re: [PHP-DEV] Re: 8.1 / Exception / Property Type / Backwards compatbility
Nikita Popov wrote on 8/26/21 14:57: > On Thu, Aug 26, 2021 at 8:34 PM Ben Ramsey wrote: > >> Nikita Popov wrote on 8/26/21 09:57: >>> Right. I at least do not plan to address this issue. If you take a >>> protected property and publicly re-export it, then any compatibility >> issues >>> are on you. >> >> This does not appear to affect only cases where one is re-exporting a >> protected property as public. >> >> Exception protected properties without type hints: >> >> * PHP <= 8.0 - https://3v4l.org/GWmrk >> * PHP 8.1 - https://3v4l.org/GWmrk/rfc >> >> >> Exception protected properties with type hints: >> >> * PHP <= 8.0 - https://3v4l.org/UX1Pa >> * PHP 8.1 - https://3v4l.org/UX1Pa/rfc >> > > These are not really meaningful examples, because you could simply not > redeclare the property at all and assign it in the constructor instead (or > preferably, let the parent constructor assign it). That's the normal way to > extend exceptions. What made the original case interesting is that the > property redeclaration isn't immediately redundant there, because it > changes visibility. If we don't want downstream users to redeclare the properties, why are they protected? Cheers, Ben signature.asc Description: OpenPGP digital signature
Re: [PHP-DEV] Re: 8.1 / Exception / Property Type / Backwards compatbility
On Thu, Aug 26, 2021 at 8:34 PM Ben Ramsey wrote: > Nikita Popov wrote on 8/26/21 09:57: > > Right. I at least do not plan to address this issue. If you take a > > protected property and publicly re-export it, then any compatibility > issues > > are on you. > > This does not appear to affect only cases where one is re-exporting a > protected property as public. > > Exception protected properties without type hints: > > * PHP <= 8.0 - https://3v4l.org/GWmrk > * PHP 8.1 - https://3v4l.org/GWmrk/rfc > > > Exception protected properties with type hints: > > * PHP <= 8.0 - https://3v4l.org/UX1Pa > * PHP 8.1 - https://3v4l.org/UX1Pa/rfc > These are not really meaningful examples, because you could simply not redeclare the property at all and assign it in the constructor instead (or preferably, let the parent constructor assign it). That's the normal way to extend exceptions. What made the original case interesting is that the property redeclaration isn't immediately redundant there, because it changes visibility. Regards, Nikita
Re: [PHP-DEV] Re: 8.1 / Exception / Property Type / Backwards compatbility
Nikita Popov wrote on 8/26/21 09:57: > Right. I at least do not plan to address this issue. If you take a > protected property and publicly re-export it, then any compatibility issues > are on you. This does not appear to affect only cases where one is re-exporting a protected property as public. Exception protected properties without type hints: * PHP <= 8.0 - https://3v4l.org/GWmrk * PHP 8.1 - https://3v4l.org/GWmrk/rfc Exception protected properties with type hints: * PHP <= 8.0 - https://3v4l.org/UX1Pa * PHP 8.1 - https://3v4l.org/UX1Pa/rfc Cheers, Ben signature.asc Description: OpenPGP digital signature
Re: [PHP-DEV] Re: 8.1 / Exception / Property Type / Backwards compatbility
On Thu, Aug 26, 2021 at 4:43 PM Dan Ackroyd wrote: > On Thu, 26 Aug 2021 at 12:42, Björn Larsson via internals > wrote: > > > > Den 2021-08-10 kl. 11:55, skrev Philip Hofstetter: > > > The following valid <= PHP 8.0 code that intends to make the $line > property > > > public is a fatal error in 8.1 > > > > > > > For method return types, we have #[ReturnTypeWillChange], but for > property > > > types 路♀️ > > > Hi, > > > > Has this been adressed / solved in some way and does it needs > > to be fixed? > > My understanding is that the two scenarios are not the same and that > there isn't much enthusiasm for 'fixing' it. > > For return types, the #[ReturnTypeWillChange] annotation is a > temporary work-around, and one that would be used by a lot of people. > It would only be used by a library until that library drops support > for older versions of PHP. > > For the exception case, the way that (for legacy reasons) PHP supports > having a public property in a child class that "overwrites" (mostly) > the protected property in the parent class, is a pretty hinkey thing > to do, and so this is likely to only affect a small number of people. > It's not obvious to me how long a bridging annotation would need to > hang around for, and it's really not obvious what the exact details of > how it would work would be. > > I'd suggest using a getter method, which would work on all relevant > versions. > Right. I at least do not plan to address this issue. If you take a protected property and publicly re-export it, then any compatibility issues are on you. If you need to keep doing this for API stability reasons, then the way to do it would be a conditional class declaration. Or to keep the scope smaller, you can conditionally declare a trait with just the property and then use it inside a larger class. Regards, Nikita
Re: [PHP-DEV] Re: 8.1 / Exception / Property Type / Backwards compatbility
Dan Ackroyd wrote on 8/26/21 09:43: > On Thu, 26 Aug 2021 at 12:42, Björn Larsson via internals > wrote: >> >> Den 2021-08-10 kl. 11:55, skrev Philip Hofstetter: >>> The following valid <= PHP 8.0 code that intends to make the $line property >>> public is a fatal error in 8.1 > >>> >>> For method return types, we have #[ReturnTypeWillChange], but for property >>> types 路♀️ > >> Hi, >> >> Has this been adressed / solved in some way and does it needs >> to be fixed? > > My understanding is that the two scenarios are not the same and that > there isn't much enthusiasm for 'fixing' it. > > For return types, the #[ReturnTypeWillChange] annotation is a > temporary work-around, and one that would be used by a lot of people. > It would only be used by a library until that library drops support > for older versions of PHP. > > For the exception case, the way that (for legacy reasons) PHP supports > having a public property in a child class that "overwrites" (mostly) > the protected property in the parent class, is a pretty hinkey thing > to do, and so this is likely to only affect a small number of people. > It's not obvious to me how long a bridging annotation would need to > hang around for, and it's really not obvious what the exact details of > how it would work would be. > > I'd suggest using a getter method, which would work on all relevant versions. > > cheers > Dan > Ack > It's interesting to note that `$line` and `$file` both behave this way in PHP 8.1, but `$code` and `$message` do not. class Foo extends Exception { protected $code = 100; protected $message = 'Hello'; protected $line = 45; protected $file = '/path/to/file.php'; } Cheers, Ben signature.asc Description: OpenPGP digital signature
Re: [PHP-DEV] Re: 8.1 / Exception / Property Type / Backwards compatbility
On Thu, 26 Aug 2021 at 12:42, Björn Larsson via internals wrote: > > Den 2021-08-10 kl. 11:55, skrev Philip Hofstetter: > > The following valid <= PHP 8.0 code that intends to make the $line property > > public is a fatal error in 8.1 > > > > For method return types, we have #[ReturnTypeWillChange], but for property > > types 路♀️ > Hi, > > Has this been adressed / solved in some way and does it needs > to be fixed? My understanding is that the two scenarios are not the same and that there isn't much enthusiasm for 'fixing' it. For return types, the #[ReturnTypeWillChange] annotation is a temporary work-around, and one that would be used by a lot of people. It would only be used by a library until that library drops support for older versions of PHP. For the exception case, the way that (for legacy reasons) PHP supports having a public property in a child class that "overwrites" (mostly) the protected property in the parent class, is a pretty hinkey thing to do, and so this is likely to only affect a small number of people. It's not obvious to me how long a bridging annotation would need to hang around for, and it's really not obvious what the exact details of how it would work would be. I'd suggest using a getter method, which would work on all relevant versions. cheers Dan Ack -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php