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

gc_collect_cycles() may interfere with using foreach to iterate objects

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • PHP Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      As reported in mongodb/mongo-php-driver#1776, calling gc_collect_cycles() may impact how properties of PHPC objects are reported when using foreach.

      To test this, I modified php_phongo_objectid_get_properties_hash() to print the value of is_temp each time it is called. I then ran the following:

      <?php
      
      $oid = new MongoDB\BSON\ObjectId;
      
      echo "foreach ObjectId:\n";
      foreach ($oid as $k => $v) {
      	printf("%s: %s\n", $k, $v);
      }
      
      printf("\ngc_collect_cycles: %d\n", gc_collect_cycles());
      
      echo "\nforeach ObjectId:\n";
      foreach ($oid as $k => $v) {
      	printf("%s: %s\n", $k, $v);
      }
      
      echo "\nvar_dump() ObjectId:\n";
      var_dump($oid);
      
      echo "\nget_object_vars() ObjectId\n";
      var_dump(get_object_vars($oid));
      

      Output:

      foreach ObjectId:
      php_phongo_objectid_get_properties_hash is_temp: 0
      php_phongo_objectid_get_properties_hash is_temp: 0
      oid: 67a0e8dcff929c245f0786c0
      php_phongo_objectid_get_properties_hash is_temp: 0
      
      gc_collect_cycles: 0
      
      foreach ObjectId:
      
      var_dump() ObjectId:
      php_phongo_objectid_get_properties_hash is_temp: 1
      object(MongoDB\BSON\ObjectId)#1 (1) {
        ["oid"]=>
        string(24) "67a0e8dcff929c245f0786c0"
      }
      
      get_object_vars() ObjectId
      php_phongo_objectid_get_properties_hash is_temp: 0
      array(1) {
        ["oid"]=>
        string(24) "67a0e8dcff929c245f0786c0"
      }
      

      var_dump() calls the get_debug_info handler, which sets is_temp: true, as expected. get_object_vars() uses the get_properties handler, which is also expected.

      It is unclear why the first foreach calls the get_properties handler three times, and then omits it entirely after the call to gc_collect_cycles.

            Assignee:
            Unassigned Unassigned
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: