Uploaded image for project: 'PHP Driver: Extension'
  1. PHP Driver: Extension
  2. PHPC-2325

Null coalesce operator doesn't silence exception when accessing non-existent BSON field

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.17.1
    • Affects Version/s: 1.17.0
    • Component/s: None
    • None

      When dealing with a class that implements the __get and __isset magic methods, the null coalesce operator will generally not invoke __get if __isset already returns false: https://3v4l.org/GBCfS.

      However, the BSON Document and PackedArray classes behave differently, in that using the coalesce operator still results in an exception:

      $document = MongoDB\BSON\Document::fromPHP([]);
      
      var_dump(isset($document->foo));
      var_dump($document->foo ?? 'fallback');
      

      This code outputs the following:

      bool(false)
      
      Fatal error: Uncaught MongoDB\Driver\Exception\RuntimeException: Could not find key "foo" in BSON document in /Users/alcaeus/Code/mongodb/mongo-php-driver/tests/bson/bson-document-property-access-002.php:8
      

      For consistency, the read_property and read_dimension handlers should not throw when the coalesce operator is used.

            Assignee:
            andreas.braun@mongodb.com Andreas Braun
            Reporter:
            andreas.braun@mongodb.com Andreas Braun
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: