php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65135 Support for non-scalar keys in foreach() does not work with SplObjectStorage
Submitted: 2013-06-26 16:09 UTC Modified: 2013-07-06 16:35 UTC
From: sebastian@php.net Assigned: nikic (profile)
Status: Wont fix Package: SPL related
PHP Version: 5.5.0 OS: Irrelevant
Private report: No CVE-ID: None
 [2013-06-26 16:09 UTC] sebastian@php.net
Description:
------------
Quoting from https://wiki.php.net/rfc/foreach-non-scalar-keys

  // NOT possible
  foreach ($objectStore as $key => $value) {
    // ...
  }
 
  // Instead you have to use
  foreach ($objectStore as $key) {
    $value = $objectStore[$key];
 
    // ...
  }

This suggests that it would work after the proposed changes. However, in PHP 5.5 (for which the RFC was accepted) this still does not work.

Test script:
---------------
<?php
$o = new SplObjectStorage;
$o->attach(new DateTime, new StdClass);

foreach ($o as $key => $value) {
    var_dump($key);
    var_dump($value);
}

Expected result:
----------------
class DateTime#2 (3) {
  public $date =>
  string(19) "2013-06-26 18:04:30"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(13) "Europe/Berlin"
}
class stdClass#3 (0) {
}

Actual result:
--------------
int(0)
class DateTime#2 (3) {
  public $date =>
  string(19) "2013-06-26 18:03:24"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(13) "Europe/Berlin"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-26 16:11 UTC] sebastian@php.net
-Assigned To: +Assigned To: nikic
 [2013-06-26 17:45 UTC] nikic@php.net
The RFC only adds the ability to have non-scalar keys. It does not change the behavior of any of our existing iterators.

I don't think we can change the SplObjectStorage behavior as it would constitute a BC break (all current loops would break.)
 [2013-07-06 16:35 UTC] nikic@php.net
-Status: Assigned +Status: Wont fix
 [2013-07-06 16:35 UTC] nikic@php.net
Closing as Wfx as I don't see how we can reasonably change this.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC