# Add leading backslash to enum and class names in var_export

- Thread: https://externals.io/message/117466
- Original archive: https://news-web.php.net/php.internals/117466
- Started by Ilija Tovilo on 2022-03-31T15:45:29+00:00
- Messages: 6
- 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

---

## #117466 — Ilija Tovilo — 2022-03-31T15:45:29+00:00

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

Hi everyone

We've had two bug reports for var_export not working for enums. The
reason for that is that var_export does not add a leading backslash to
names of enums. This will cause them to be resolved as
`\CurrentNamespace\EnumNamespace\EnumName` instead of just
`\EnumNamespace\EnumName`.

enum A {
    case B;
}

echo var_export(A::B, true), "\n";
> A::B


This is a problem for things like Doctrines ProxyGenerator that embeds
the result of var_export in classes with namespaces
(https://github.com/doctrine/common/pull/955). The Doctrine team
resolved this by adding a `use` of the enum at the top of the file but
that really shouldn't be necessary.

The same issue already exists for classes.

class C {}

echo var_export(new C(), true), "\n";
> C::__set_state(array(
> ))

https://bugs.php.net/bug.php?id=64554

Marco Pivetta created a PR that adds leading backslash to all class or
enum names of var_export. Adding a backslash will make the code work
both inside or outside namespaces.
https://github.com/php/php-src/pull/8233

To avoid disruption, I'm proposing to merge this into PHP 8.2 only.
Unless anybody has any objections to this change I will merge it in a
few weeks.

Ilija

---

## #117467 — Larry Garfield — 2022-03-31T15:50:09+00:00

URL: https://externals.io/message/117466#117467 · In reply to: #117466

> Hi everyone
>
> We've had two bug reports for var_export not working for enums. The
> reason for that is that var_export does not add a leading backslash to
> names of enums. This will cause them to be resolved as
> `\CurrentNamespace\EnumNamespace\EnumName` instead of just
> `\EnumNamespace\EnumName`.
>
> enum A {
>     case B;
> }
>
> echo var_export(A::B, true), "\n";
> > […]
>
>
> This is a problem for things like Doctrines ProxyGenerator that embeds
> the result of var_export in classes with namespaces
> (https://github.com/doctrine/common/pull/955). The Doctrine team
> resolved this by adding a `use` of the enum at the top of the file but
> that really shouldn't be necessary.
>
> The same issue already exists for classes.
>
> class C {}
>
> echo var_export(new C(), true), "\n";
> > […]
>
> https://bugs.php.net/bug.php?id=64554
>
> Marco Pivetta created a PR that adds leading backslash to all class or
> enum names of var_export. Adding a backslash will make the code work
> both inside or outside namespaces.
> https://github.com/php/php-src/pull/8233
>
> To avoid disruption, I'm proposing to merge this into PHP 8.2 only.
> Unless anybody has any objections to this change I will merge it in a
> few weeks.
>
> Ilija

What would be the disruption on 8.1?  It wouldn't break the existing `use` trick, and I doubt anyone is leveraging this deliberately.

--Larry Garfield

---

## #117468 — Nicolas Grekas — 2022-03-31T15:56:08+00:00

URL: https://externals.io/message/117466#117468 · In reply to: #117467

Le jeu. 31 mars 2022 à 17:50, Larry Garfield <larry@garfieldtech.com> a
écrit :

> 
> > […]
>
> What would be the disruption on 8.1?  It wouldn't break the existing `use`
> trick, and I doubt anyone is leveraging this deliberately.
>
>
It could break fixtures that ppl might use to compare the two outputs of
 `var_export()`.

---

## #117469 — Christoph M. Becker — 2022-03-31T16:03:16+00:00

URL: https://externals.io/message/117466#117469 · In reply to: #117466

> We've had two bug reports for var_export not working for enums. The
> reason for that is that var_export does not add a leading backslash to
> names of enums. This will cause them to be resolved as
> `\CurrentNamespace\EnumNamespace\EnumName` instead of just
> `\EnumNamespace\EnumName`.
>
> enum A {
>     case B;
> }
>
> echo var_export(A::B, true), "\n";
> > […]
>
>
> This is a problem for things like Doctrines ProxyGenerator that embeds
> the result of var_export in classes with namespaces
> (https://github.com/doctrine/common/pull/955). The Doctrine team
> resolved this by adding a `use` of the enum at the top of the file but
> that really shouldn't be necessary.
>
> The same issue already exists for classes.
>
> class C {}
>
> echo var_export(new C(), true), "\n";
> > […]
>
> https://bugs.php.net/bug.php?id=64554
>
> Marco Pivetta created a PR that adds leading backslash to all class or
> enum names of var_export. Adding a backslash will make the code work
> both inside or outside namespaces.
> https://github.com/php/php-src/pull/8233
>
> To avoid disruption, I'm proposing to merge this into PHP 8.2 only.
> Unless anybody has any objections to this change I will merge it in a
> few weeks.

It might be worthwhile to point out that this issue already came up
years ago, and after some discussion[1] the idea was rejected back then.

That said, I'm not against changing this now, but would not want to do
that for any of the stable PHP versions.  Targeting PHP 8.2 is fine for me.

[1] <https://externals.io/message/67008>

--
Christoph M. Becker

---

## #117472 — Derick Rethans — 2022-03-31T20:27:32+00:00

URL: https://externals.io/message/117466#117472 · In reply to: #117466

>To avoid disruption, I'm proposing to merge this into PHP 8.2 only.
>Unless anybody has any objections to this change I will merge it in a
>few weeks.

As original author of var_export, I believe this is good to merge, but for PHP 8.2+ only. 

cheers 
Derick

---

## #117532 — Ilija Tovilo — 2022-04-14T14:37:22+00:00

URL: https://externals.io/message/117466#117532 · In reply to: #117466

Hi everyone

> Marco Pivetta created a PR that adds leading backslash to all class or
> enum names of var_export. Adding a backslash will make the code work
> both inside or outside namespaces.
> https://github.com/php/php-src/pull/8233
>
> To avoid disruption, I'm proposing to merge this into PHP 8.2 only.
> Unless anybody has any objections to this change I will merge it in a
> few weeks.

Just another reminder. If there are no objections I will merge this next week.

Ilija
