xref: /PHP-8.1/ext/session/tests/bug79031.phpt (revision 18521210)
1--TEST--
2Bug #79031: Session unserialization problem
3--EXTENSIONS--
4session
5--INI--
6error_reporting=E_ALL&~E_DEPRECATED
7--FILE--
8<?php
9
10class SerializableClass implements Serializable {
11    public $sharedProp;
12    public function __construct($prop)
13    {
14        $this->sharedProp = $prop;
15    }
16    public function __set($key, $value)
17    {
18        $this->$key = $value;
19    }
20    public function serialize()
21    {
22        return serialize(get_object_vars($this));
23    }
24    public function unserialize($data)
25    {
26        $ar = unserialize($data);
27        if ($ar === false) {
28            return;
29        }
30        foreach ($ar as $k => $v) {
31            $this->__set($k, $v);
32        }
33    }
34}
35
36// Shared object that acts as property of two another objects stored in session
37$testPropertyObj = new stdClass();
38$testPropertyObj->name = 'test';
39
40// Two instances of \SerializableClass that shares property
41$sessionObject = [
42    'obj1' => new SerializableClass($testPropertyObj),
43    'obj2' => new SerializableClass($testPropertyObj),
44];
45session_start();
46$_SESSION = $sessionObject;
47
48$sessionString = session_encode();
49session_decode($sessionString);
50echo $sessionString;
51echo "\n\n";
52var_dump($_SESSION);
53
54?>
55--EXPECT--
56obj1|C:17:"SerializableClass":65:{a:1:{s:10:"sharedProp";O:8:"stdClass":1:{s:4:"name";s:4:"test";}}}obj2|C:17:"SerializableClass":28:{a:1:{s:10:"sharedProp";r:3;}}
57
58array(2) {
59  ["obj1"]=>
60  object(SerializableClass)#4 (1) {
61    ["sharedProp"]=>
62    object(stdClass)#5 (1) {
63      ["name"]=>
64      string(4) "test"
65    }
66  }
67  ["obj2"]=>
68  object(SerializableClass)#6 (1) {
69    ["sharedProp"]=>
70    object(stdClass)#5 (1) {
71      ["name"]=>
72      string(4) "test"
73    }
74  }
75}
76