PHP Bugs  
php.net | support | documentation | report a bug | advanced search | search howto | statistics | login

go to bug id or search bugs for  

Bug #32983 Bogus error message when using ArrayAccess / References
Submitted:9 May 2005 12:38pm UTC Modified: 16 Aug 2005 11:30pm UTC
From:jason at amp-design dot net Assigned to:helly
Status:Closed Category:SPL related
Version:5.0.3 OS:*
Votes:2 Avg. Score:3.5 ± 1.5 Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%) Same OS:1 (100.0%)
View/Vote Developer Edit Submission

[9 May 2005 12:38pm UTC] jason at amp-design dot net
Description:
------------
I'm not 100% sure this is considered a "bug" as such, anyway, I thought
I'd point it out, and let you decide. It's more a case of the error
message being a little fuzzy.

When trying to assign an item by reference by using the reference
operator, &, to an element inside a class that implements ArrayAccess
produces a werid error message.

Admittedly, the code I've provided is probably not valid PHP code,
because the nature of the ArrayAccess interface means that data is
assigned and returned from offsetSet and offsetGet by value, so using
refernces should probably not work.

However, the when you do try this, you get an error about about post/pre
increment/decrement. I'm not sure what this refers to, but it doesn't
seem to be very descriptive.

Reproduce code:
---------------
<?php

class ArrayAccessImpl implements ArrayAccess {
	private $data = array();

	public function offsetUnset($index) {}

	public function offsetSet($index, $value) {
		$this->data[$index] = $value;
	}

	public function offsetGet($index) {
		return $this->data[$index];
	}

	public function offsetExists($index) {
		return isset($this->data[$index]);
	}
}

$data = new ArrayAccessImpl();
$test = 'some data';
$data['element'] = &$test;

?>

Expected result:
----------------
Unsure, probably an error message relating to the fact ArrayAccess
objects can not assign by reference

Actual result:
--------------
Fatal error: Objects used as arrays in post/pre increment/decrement must
return values by reference
[9 May 2005 7:05pm UTC] helly@php.net
Actually at the moment this is a known issue which we cannot fix
appropriate right away. But we are working on the matter.
[16 Aug 2005 7:44am UTC] sslotnick at gmail dot com
I have a similar but different reproduction using two dimensional arrays
instead of explicitly setting a reference.

class ArrayAccessImpl implements ArrayAccess {
  private $data = array();

  public function offsetUnset($index) {}

  public function offsetSet($index, $value) {
    $this->data[$index] = $value;
  }

  public function offsetGet($index) {
    return $this->data[$index];
  }

  public function offsetExists($index) {
    return isset($this->data[$index]);
  }
}

$data = new ArrayAccessImpl();
$data['element'] = array();
$data['element']['element2'] = "hi";
[16 Aug 2005 11:30pm UTC] helly@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php

We found out that this is not solvable without blowing up the interface
and creating a BC or providing an additional interface to support
references and thereby creating an internal nightmare - actually i don't
see a way we can make that work ever. Thus we decided to enforce the
original design and disallow references completley. Also the error
message should have changed with 5.0.4 or is being changed with
5.0.5/5.1 at least

RSS feed | show source 

PHP Copyright © 2001-2009 The PHP Group
All rights reserved.
Last updated: Sat Nov 21 10:30:49 2009 UTC