Skip to content

unserialize can only work once #76

@delbertooo

Description

@delbertooo

The so called "magic" from EnumSerializableTrait can only work once. If unserialize is called more than one time, it can't be compared by reference anymore.

This isn't your fault at all. It's just not possible to build this in php right now.

IMO the serializable support should be entirely dropped. It has so many possabilities to create side effects and nearly no safe usages. The enums should throw errors if serialized / unserialized. Maybe there could be a serializable enum class, which isn't comparable via reference.

How to reproduce

From 7899614f0dc543e3ad2f3f2304d2ed257d7bc7b6 Mon Sep 17 00:00:00 2001
From: Robert Worgul <robert.worgul@scitotec.de>
Date: Fri, 4 Nov 2016 15:10:33 +0100
Subject: [PATCH] test unserialize works multiple times

---
 tests/MabeEnumTest/EnumSerializableTraitTest.php | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tests/MabeEnumTest/EnumSerializableTraitTest.php b/tests/MabeEnumTest/EnumSerializableTraitTest.php
index 65e6280..01ce977 100644
--- a/tests/MabeEnumTest/EnumSerializableTraitTest.php
+++ b/tests/MabeEnumTest/EnumSerializableTraitTest.php
@@ -49,6 +49,9 @@ class EnumSerializableTraitTest extends TestCase

         // check if it's the same instance
         $this->assertSame($enum, $unserialized);
+
+        $unserialized2 = unserialize($serialized);
+        $this->assertSame($enum, $unserialized2);
     }

     public function testUnserializeThrowsRuntimeExceptionOnUnknownValue()
--
2.7.4

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions