diff --git a/ext/spl/spl_heap.c b/ext/spl/spl_heap.c index a3249171f4d..afa95ebaa3f 100644 --- a/ext/spl/spl_heap.c +++ b/ext/spl/spl_heap.c @@ -397,7 +397,13 @@ static zend_object_value spl_heap_object_new_ex(zend_class_entry *class_type, sp intern->ce_get_iterator = other->ce_get_iterator; if (clone_orig) { + int i; intern->heap = spl_ptr_heap_clone(other->heap TSRMLS_CC); + for (i = 0; i < intern->heap->count; ++i) { + if (intern->heap->elements[i]) { + Z_ADDREF_P((zval *)intern->heap->elements[i]); + } + } } else { intern->heap = other->heap; } diff --git a/ext/spl/tests/heap_001.phpt b/ext/spl/tests/heap_001.phpt index eb189d8f4ae..da4dde8ad2e 100644 --- a/ext/spl/tests/heap_001.phpt +++ b/ext/spl/tests/heap_001.phpt @@ -32,7 +32,9 @@ $b = 4; $h->insert($b); $b = 5; +$h2 = clone $h; echo $h->extract()."\n"; +echo $h2->extract()."\n"; ?> ===DONE=== @@ -47,4 +49,5 @@ Exception: Can't extract from an empty heap 0 -- 4 +4 ===DONE=== diff --git a/ext/spl/tests/heap_011.phpt b/ext/spl/tests/heap_011.phpt new file mode 100644 index 00000000000..1689abfae1a --- /dev/null +++ b/ext/spl/tests/heap_011.phpt @@ -0,0 +1,31 @@ +--TEST-- +SPL: SplHeap with overriden compare() +--FILE-- +insert(1); +$h->insert(6); +$h->insert(5); +$h->insert(2); +var_dump($h->top()); + +class SplMaxHeap2 extends SplMaxHeap { + public function compare($a, $b) { + return -parent::compare($a,$b); + } +} +$h = new SplMaxHeap2(); +$h->insert(1); +$h->insert(6); +$h->insert(5); +$h->insert(2); +var_dump($h->top()); +?> +--EXPECT-- +int(6) +int(1)