Skip to content

Commit 706b67e

Browse files
bug symfony#52759 [VarExporter] Fix serializing objects that implement __sleep() and that are made lazy (nicolas-grekas)
This PR was merged into the 6.3 branch. Discussion ---------- [VarExporter] Fix serializing objects that implement __sleep() and that are made lazy | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT Spotted while working on lazy objects with `@arnaud`-lb Commits ------- bbe8848 [VarExporter] Fix serializing objects that implement __sleep() and that are made lazy
2 parents 9145a88 + bbe8848 commit 706b67e

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/Symfony/Component/VarExporter/LazyGhostTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public function __serialize(): array
355355
$data = [];
356356

357357
foreach (parent::__sleep() as $name) {
358-
$value = $properties[$k = $name] ?? $properties[$k = "\0*\0$name"] ?? $properties[$k = "\0$scope\0$name"] ?? $k = null;
358+
$value = $properties[$k = $name] ?? $properties[$k = "\0*\0$name"] ?? $properties[$k = "\0$class\0$name"] ?? $properties[$k = "\0$scope\0$name"] ?? $k = null;
359359

360360
if (null === $k) {
361361
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $name), \E_USER_NOTICE);

src/Symfony/Component/VarExporter/LazyProxyTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public function __serialize(): array
295295
$data = [];
296296

297297
foreach (parent::__sleep() as $name) {
298-
$value = $properties[$k = $name] ?? $properties[$k = "\0*\0$name"] ?? $properties[$k = "\0$scope\0$name"] ?? $k = null;
298+
$value = $properties[$k = $name] ?? $properties[$k = "\0*\0$name"] ?? $properties[$k = "\0$class\0$name"] ?? $properties[$k = "\0$scope\0$name"] ?? $k = null;
299299

300300
if (null === $k) {
301301
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $name), \E_USER_NOTICE);

src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,13 @@ public function testMagicClass(MagicClass $instance)
146146
$instance->bar = 123;
147147
$serialized = serialize($instance);
148148
$clone = unserialize($serialized);
149-
$this->assertSame(123, $clone->bar);
149+
150+
if ($instance instanceof ChildMagicClass) {
151+
// ChildMagicClass redefines the $data property but not the __sleep() method
152+
$this->assertFalse(isset($clone->bar));
153+
} else {
154+
$this->assertSame(123, $clone->bar);
155+
}
150156
}
151157

152158
public static function provideMagicClass()

0 commit comments

Comments
 (0)