php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77681 Typecasting with null coalescing operator and ArrayAccess
Submitted: 2019-02-28 16:36 UTC Modified: 2019-02-28 18:08 UTC
From: palmer dot andy at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.3.2 OS:
Private report: No CVE-ID: None
 [2019-02-28 16:36 UTC] palmer dot andy at gmail dot com
Description:
------------
This is very similar to https://bugs.php.net/bug.php?id=71731 - the only difference being that I am typecasting.

It seems when typecasting is used, offsetExists is never called. It doesn't matter whether I try to typecast to string or int.

Test script:
---------------
<?php

$data = new class implements \ArrayAccess
{
    public function offsetExists($offset)
    {
        echo "offsetExists\n";
    }

    public function offsetGet($offset)
    {
        echo "offsetGet\n";
    }

    public function offsetSet($offset, $value)
    {
    }

    public function offsetUnset($offset)
    {
    }

};
echo "null coalescing operator:\n";
$data['foo'] ?? null;
echo "null coalescing operator (typecast):\n";
(int)$data['foo'] ?? null;


Expected result:
----------------
null coalescing operator:
offsetExists
null coalescing operator (typecast):
offsetExists

Actual result:
--------------
null coalescing operator:
offsetExists
null coalescing operator (typecast):
offsetGet

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-02-28 18:08 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-02-28 18:08 UTC] requinix@php.net
http://php.net/manual/en/language.operators.precedence.php
Typecasts have higher precedence than ??
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Apr 20 10:01:23 2021 UTC