diff --git a/src/Symfony/Component/VarExporter/Internal/Exporter.php b/src/Symfony/Component/VarExporter/Internal/Exporter.php index c46eb50aa988d..7141b4e6d96c1 100644 --- a/src/Symfony/Component/VarExporter/Internal/Exporter.php +++ b/src/Symfony/Component/VarExporter/Internal/Exporter.php @@ -140,7 +140,7 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount $i = 0; $n = (string) $name; if ('' === $n || "\0" !== $n[0]) { - $c = 'stdClass'; + $c = \PHP_VERSION_ID >= 80100 && $reflector->hasProperty($n) && ($p = $reflector->getProperty($n))->isReadOnly() ? $p->class : 'stdClass'; } elseif ('*' === $n[1]) { $n = substr($n, 3); $c = $reflector->getProperty($n)->class; diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/FooReadonly.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/FooReadonly.php new file mode 100644 index 0000000000000..8e41de95958bc --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/FooReadonly.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarExporter\Tests\Fixtures; + +class FooReadonly +{ + public function __construct( + public readonly string $name, + public readonly string $value, + ) { + } +} diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/readonly.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/readonly.php new file mode 100644 index 0000000000000..3b3db27305859 --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/readonly.php @@ -0,0 +1,20 @@ + [ + 'name' => [ + 'k', + ], + 'value' => [ + 'v', + ], + ], + ], + $o[0], + [] +); diff --git a/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php b/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php index 407f25af1891f..f90737da2e8cf 100644 --- a/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php +++ b/src/Symfony/Component/VarExporter/Tests/VarExporterTest.php @@ -16,6 +16,7 @@ use Symfony\Component\VarExporter\Exception\ClassNotFoundException; use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException; use Symfony\Component\VarExporter\Internal\Registry; +use Symfony\Component\VarExporter\Tests\Fixtures\FooReadonly; use Symfony\Component\VarExporter\Tests\Fixtures\FooSerializable; use Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum; use Symfony\Component\VarExporter\Tests\Fixtures\MySerializable; @@ -244,9 +245,12 @@ public function provideExport() yield ['php74-serializable', new Php74Serializable()]; - if (\PHP_VERSION_ID >= 80100) { - yield ['unit-enum', [FooUnitEnum::Bar], true]; + if (\PHP_VERSION_ID < 80100) { + return; } + + yield ['unit-enum', [FooUnitEnum::Bar], true]; + yield ['readonly', new FooReadonly('k', 'v')]; } public function testUnicodeDirectionality()