Skip to content

SplPriorityQueue does not support serialization #14402

Closed

Description

@nikic

Description

The following code:

<?php
$bar = new \SplPriorityQueue();
$bar->insert('a', 1);
var_dump($bar);
var_dump(unserialize(serialize($bar)));

Resulted in this output:

object(SplPriorityQueue)#1 (3) {
  ["flags":"SplPriorityQueue":private]=>
  int(1)
  ["isCorrupted":"SplPriorityQueue":private]=>
  bool(false)
  ["heap":"SplPriorityQueue":private]=>
  array(1) {
    [0]=>
    array(2) {
      ["data"]=>
      string(1) "a"
      ["priority"]=>
      int(1)
    }
  }
}
object(SplPriorityQueue)#2 (3) {
  ["flags":"SplPriorityQueue":private]=>
  int(1)
  ["isCorrupted":"SplPriorityQueue":private]=>
  bool(false)
  ["heap":"SplPriorityQueue":private]=>
  array(0) {
  }
}

But I expected this output instead:


object(SplPriorityQueue)#1 (3) {
  ["flags":"SplPriorityQueue":private]=>
  int(1)
  ["isCorrupted":"SplPriorityQueue":private]=>
  bool(false)
  ["heap":"SplPriorityQueue":private]=>
  array(1) {
    [0]=>
    array(2) {
      ["data"]=>
      string(1) "a"
      ["priority"]=>
      int(1)
    }
  }
}

object(SplPriorityQueue)#2 (3) {
  ["flags":"SplPriorityQueue":private]=>
  int(1)
  ["isCorrupted":"SplPriorityQueue":private]=>
  bool(false)
  ["heap":"SplPriorityQueue":private]=>
  array(1) {
    [0]=>
    array(2) {
      ["data"]=>
      string(1) "a"
      ["priority"]=>
      int(1)
    }
  }
}

Disclaimer: I don't have any need for this myself, just cross-posting an issue originally reported against apcu in krakjoe/apcu#505. It does seem weird that an SPL container cannot be serialized (or rather, loses data on serialization) though.

PHP Version

All

Operating System

No response

Activity

  1. MorganLOCode commented on Jun 1, 2024

    @MorganLOCode

    Ditto for Spl{Min,Max}Heap, and other concrete SplHeap classes (though SplPriorityQueue isn't explicitly such, it does use a max heap internally, presumably the same code).

  2. added a commit that references this issue on Sep 22, 2025
    e6c07b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions