[PHP-NOTES] note 130225 added to arrayaccess.offsetexists

[email protected] ("[email protected]")
Newsgroups php.notes
Message-ID <[email protected]>
Please note something:

The docs explain clearly that this method is called when "isset()" or "empty()" are called on the object's key.

This means that there is a huge difference in your custom implementation when you have an internal array on which you choose to call either "isset()" or "array_key_exists()".

Even though the method says "offsetExists", it is *not* supposed to be used only when the offset exists, because this is not at all the behavior of neither "isset" nor "empty" internally.

This means you can have issues like this (more explanations below):

<?php

class Value {
    public function __construct(
        public string $value,
    ) {
    }
}

class MyArray implements ArrayAccess {
    private array $internal = [];

    public function offsetExists(mixed $offset): bool
    {
        return array_key_exists($offset, $this->internal);
    }

    // ... rest of the implementation
    public function offsetGet(mixed $offset): mixed
    {
        return $this->offsetExists($offset) ? $this->internal[$offset] : null;
    }

    public function offsetSet(mixed $offset, mixed $value): void
    {
        if (is_null($offset)) {
            $this->internal[] = $value;
        } else {
            $this->internal[$offset] = $value;
        }
    }

    public function offsetUnset(mixed $offset): void
    {
        unset($this->internal[$offset]);
    }
}

$object = new MyArray();
$object['key'] = null;

// This is where the error occurs:
// PHP Fatal error:  Uncaught TypeError: Value::__construct(): Argument #1 ($value) must be of type string, null given
$otherValue = isset($object['key']) ? new Value($object['key']) : null;
?>

The thing here is that we have some code that cannot use the "??" operator because we need the output of the "isset" call to return true, and only then we want to use.

With a real array, this should be fairly common because we know how "isset" works.

However, since the "offsetExists" method has a lot of different implementations in PHP libaries, you should *not* trust the output in "isset" with objects implementing ArrayAccess.

A workaround is to create an intermediate variable and run "isset()" on it:

<?php

// Before
$otherValue = isset($arrayObject['key']) ? new Value($arrayObject['key']) : null;

// After
$rawValue = $arrayObject['key'] ?? null;
$otherValue = isset($rawValue) ? new Value($rawValue) : null;

?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 2a01:cb14:805e:f500:5117:49ca:3a6c:5cb3)
----
Manual Page -- https://php.net/manual/en/arrayaccess.offsetexists.php
Edit        -- https://main.php.net/note/edit/130225
Del: integrated  -- https://main.php.net/note/delete/130225/integrated
Del: useless     -- https://main.php.net/note/delete/130225/useless
Del: bad code    -- https://main.php.net/note/delete/130225/bad+code
Del: spam        -- https://main.php.net/note/delete/130225/spam
Del: non-english -- https://main.php.net/note/delete/130225/non-english
Del: in docs     -- https://main.php.net/note/delete/130225/in+docs
Del: other reasons-- https://main.php.net/note/delete/130225
Reject      -- https://main.php.net/note/reject/130225
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.