php / php/php-src

WeakMap::offsetExists return false if value is null

Open
#8,437 11 comments 0 reactions 1 assignee View on GitHub

@Girgias is already working on this.

Since Mar 8, 2024.

Bug Category: Engine Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php

$wm = new \WeakMap;
$os = new \SplObjectStorage;

$obj1 = new \stdClass;
$obj2 = new \stdClass;

$wm[$obj1] = null;
$wm[$obj2] = true;

$os[$obj1] = null;
$os[$obj2] = true;


var_dump($wm->offsetExists($obj1));
var_dump($wm->offsetExists($obj2));

var_dump($os->offsetExists($obj1));
var_dump($os->offsetExists($obj2));


foreach ($wm as $key => $value) {
    if ($key === $obj1) {
        echo 'key $obj1 is always here';
    }
}

Resulted in this output:

bool(false)
bool(true)
bool(true)
bool(true)
key $obj1 is always here

But I expected this output instead:

bool(true)
bool(true)
bool(true)
bool(true)
key $obj1 is always here
PHP Version

PHP 8.1.5

Operating System

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.