Skip to content

Handling Reference Types

Frank edited this page Jan 17, 2022 · 1 revision

Tracking Objects

basic types are not tracked see ext/standard/var.c, around line 610

if (!is_ref && Z_TYPE_P(var) != IS_OBJECT) {
    return 0;
}

PHP does recognize basic type implicit references though

php > var_dump(unserialize("a:2:{i:0;i:1;i:1;R:2;}"));
array(2) {
  [0]=>
  &int(1)
  [1]=>
  &int(1)
}
php > var_dump(unserialize("a:2:{i:0;i:1;i:1;r:2;}"));
PHP Notice:  unserialize(): Error at offset 21 of 22 bytes in php shell code on line 1
Notice: unserialize(): Error at offset 21 of 22 bytes in php shell code on line 1
bool(false)

SerialzeValueError: Invalid Reference

PHP would dereference and serialize the value normally.
since people would want references explicitly when writing Python scripts, an error is raised here.
though that brings up the problem of ordering, take the following example:

php > class Obj{};
php > $a = new Obj();
php > $b = &$a;
php > echo serialize(array($a, $b));
a:2:{i:0;O:3:"Obj":0:{}i:1;r:2;}
php > echo serialize(array($b, $a)); # this would raise an error in libphpserialize
a:2:{i:0;O:3:"Obj":0:{}i:1;r:2;}
Clone this wiki locally