Skip to content

WeakMap::offsetExists return false if value is null #8437

Description

@julien-boudry

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions