From:             puts dot email at gmail dot com
Operating system: N/A
PHP version:      5.3.0alpha1
PHP Bug Type:     Feature/Change Request
Bug description:  A request for foreach to be key-type agnostic.

Description:
------------
A request for foreach to be key-type agnostic (ideally, assoc's would be)
so that the following is possible:

<?php 
class Hash implements ArrayAccess, Iterator {
  private $key = array();
  private $val = array();

  public function offsetGet($key) {
    $offset = array_search($key, $this->key);
    return $this->val[$offset];
  }

  public function offsetSet($key, $val) {
    $offset = array_search($key, $this->key);
    if ($offset === false) {
      $this->key[] = $key;
      $this->val[] = $val;
    } else {
      $this->val[$offset] = $val;
    }
  }

  public function offsetExists($key) {
   return in_array($key, $this->key);
  }

  public function offsetUnset($key) {
    $offset = array_search($key, $this->key);
    unset($this->key[$offset], $this->val[$offset]);
  }

  public function rewind() {
    reset($this->key);
    reset($this->val);
  }

  public function key() {
    return current($this->key);
  }

  public function current() {
    return current($this->val);
  }

  public function next() {
    next($this->key);
    return next($this->val);
  }

  public function valid() {
    return is_int(key($this->key));
  }
}

$h = new hash;
$o0 = new stdclass;
$o1 = new stdclass;
$o2 = new stdclass;
$o3 = new stdclass;

foreach (array($o0, $o1, $o2, $o3) as $i => $o) {
  $o->name = "o" . $i;
}

$h[$o0] = $o1;
$h[$o1] = $o2;
$h[$o2] = $o3;

foreach ($h as $key => $val) {}

?>


Reproduce code:
---------------
See description.

Expected result:
----------------
foreach ($h as $key => $val) {
  # $key === $o[012]
  # $val === $o[123]
}

Actual result:
--------------
foreach ($h as $key => $val) {
  # Warning:  Illegal type returned from Hash::key()
  # $key === 0
  # $val === $o[123]
}

-- 
Edit bug report at http://bugs.php.net/?id=45684&edit=1
-- 
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=45684&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): 
http://bugs.php.net/fix.php?id=45684&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=45684&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=45684&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=45684&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=45684&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=45684&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=45684&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=45684&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=45684&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=45684&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=45684&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=45684&r=globals
PHP 4 support discontinued:   http://bugs.php.net/fix.php?id=45684&r=php4
Daylight Savings:             http://bugs.php.net/fix.php?id=45684&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=45684&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=45684&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=45684&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=45684&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=45684&r=mysqlcfg

Reply via email to