# 8.1 / Exception / Property Type / Backwards compatbility

- Thread: https://externals.io/message/115683
- Original archive: https://news-web.php.net/php.internals/115683
- Started by Philip Hofstetter on 2021-08-10T09:55:12+00:00
- Messages: 13
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #115683 — Philip Hofstetter — 2021-08-10T09:55:12+00:00

URL: https://externals.io/message/115683#115683

Hello

The following valid <= PHP 8.0 code that intends to make the $line property
public  is a fatal error in 8.1

class FooException extends Exception {
    public $line;
}

However, the fixed code for 8.1:

class FooException extends Exception {
    public int $line;
}

Is a fatal error in <= 8.0

Is there a way to create a class that makes the $line property public
that’s compatible with all versions of PHP without requiring conditional
declaration of the class?

For method return types, we have #[ReturnTypeWillChange], but for property
types 🤷‍♀️

Philip

---

## #115852 — Björn Larsson via internals — 2021-08-26T11:42:48+00:00

URL: https://externals.io/message/115683#115852 · In reply to: #115683

Den 2021-08-10 kl. 11:55, skrev Philip Hofstetter:
> Hello
> 
> The following valid <= PHP 8.0 code that intends to make the $line property
> public  is a fatal error in 8.1
> 
> class FooException extends Exception {
>      public $line;
> }
> 
> However, the fixed code for 8.1:
> 
> class FooException extends Exception {
>      public int $line;
> }
> 
> Is a fatal error in <= 8.0
> 
> Is there a way to create a class that makes the $line property public
> that’s compatible with all versions of PHP without requiring conditional
> declaration of the class?
> 
> For method return types, we have #[ReturnTypeWillChange], but for property
> types 🤷‍♀️
> 
> Philip
> 
Hi,

Has this been adressed / solved in some way and does it needs
to be fixed?

I mean we are approaching RC1.

r//Björn L

---

## #115858 — Ben Ramsey — 2021-08-26T14:42:36+00:00

URL: https://externals.io/message/115683#115858 · In reply to: #115683

Philip Hofstetter wrote on 8/10/21 04:55:
> Hello
> 
> The following valid <= PHP 8.0 code that intends to make the $line property
> public  is a fatal error in 8.1
> 
> class FooException extends Exception {
>     public $line;
> }
> 
> However, the fixed code for 8.1:
> 
> class FooException extends Exception {
>     public int $line;
> }
> 
> Is a fatal error in <= 8.0
> 
> Is there a way to create a class that makes the $line property public
> that’s compatible with all versions of PHP without requiring conditional
> declaration of the class?
> 
> For method return types, we have #[ReturnTypeWillChange], but for property
> types 🤷‍♀️


This sounds like a bug. I don't see this reported yet in the bug
tracker. Please open an issue at https://bugs.php.net

Cheers,
Ben

---

## #115859 — Dan Ackroyd — 2021-08-26T14:43:05+00:00

URL: https://externals.io/message/115683#115859 · In reply to: #115852

On Thu, 26 Aug 2021 at 12:42, Björn Larsson via internals
<internals@lists.php.net> wrote:
>
> Den 2021-08-10 kl. 11:55, skrev Philip Hofstetter:
> > […]
....
> > […]

> 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

---

## #115860 — Ben Ramsey — 2021-08-26T14:53:54+00:00

URL: https://externals.io/message/115683#115860 · In reply to: #115859

Dan Ackroyd wrote on 8/26/21 09:43:
> On Thu, 26 Aug 2021 at 12:42, Björn Larsson via internals
> <internals@lists.php.net> wrote:
> > […]
> ....
> > […]
> 
> > […]
> 
> 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

---

## #115861 — Nikita Popov — 2021-08-26T14:57:11+00:00

URL: https://externals.io/message/115683#115861 · In reply to: #115859

> On Thu, 26 Aug 2021 at 12:42, Björn Larsson via internals
> <internals@lists.php.net> wrote:
> > […]
> property
> > […]
> ....
> > […]
> property
> > […]
>
> > […]
>
> 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

---

## #115864 — Ben Ramsey — 2021-08-26T18:34:39+00:00

URL: https://externals.io/message/115683#115864 · In reply to: #115861

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

---

## #115865 — Björn Larsson via internals — 2021-08-26T19:46:43+00:00

URL: https://externals.io/message/115683#115865 · In reply to: #115864

Den 2021-08-26 kl. 20:34, skrev Ben Ramsey:
> Nikita Popov wrote on 8/26/21 09:57:
> > […]
> 
> 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
> 
So it looks like we have a bug here since the same code behaves
differently between 8.0 and 8.1.

I don't think it's obvoius that by adding a type hint we get
different behaviour for the "same" code in 8.0 vs 8.1.

The reason I brought this up is that in an Open source library
it's discussed how to solve this. Loading different versions of
the libray feels clunky.

r//Björn L

---

## #115866 — Nikita Popov — 2021-08-26T19:57:14+00:00

URL: https://externals.io/message/115683#115866

> Nikita Popov wrote on 8/26/21 09:57:
> > […]
> issues
> > […]
>
> 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

---

## #115878 — drealecs@gmail.com — 2021-08-27T07:34:26+00:00

URL: https://externals.io/message/115683#115878 · In reply to: #115683

On Tue, Aug 10, 2021 at 12:55 PM Philip Hofstetter <
phofstetter@sensational.ch> wrote:

> Hello
>
> The following valid <= PHP 8.0 code that intends to make the $line property
> public  is a fatal error in 8.1
>
> class FooException extends Exception {
>     public $line;
> }
>
> However, the fixed code for 8.1:
>
> class FooException extends Exception {
>     public int $line;
> }
>
> Is a fatal error in <= 8.0
>
> Is there a way to create a class that makes the $line property public
> that’s compatible with all versions of PHP without requiring conditional
> declaration of the class?
>
>
Not sure exactly why you need to $line property public.
If you need to be able to read it, you can use __get.
If you also need to write it, as that also worked, you can also use __set.

So an example that offers the same interface/interaction possible would be:
https://3v4l.org/tHhEL#v8.0.9
https://3v4l.org/tHhEL/rfc#vgit.master

But of course, when you look at the object with reflection, or how var_dump
prints it, the property will still be protected.

Regards,
Alex

---

## #115879 — Ben Ramsey — 2021-08-27T15:34:37+00:00

URL: https://externals.io/message/115683#115879 · In reply to: #115866

Nikita Popov wrote on 8/26/21 14:57:
> 
> 
> > […]
> 
> 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

---

## #115880 — Nikita Popov — 2021-08-27T15:40:41+00:00

URL: https://externals.io/message/115683#115880

> Nikita Popov wrote on 8/26/21 14:57:
> > […]
> (or
> > […]
> to
> > […]
>
> 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

---

## #115881 — Ben Ramsey — 2021-08-27T15:57:28+00:00

URL: https://externals.io/message/115683#115881 · In reply to: #115880

Nikita Popov wrote on 8/27/21 10:40:
> > […]
> 
> 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
