|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-07-16 23:37 UTC] public at grik dot net
Description:
------------
Slightly modifying a Harry Fuecks's sample from sitepoint.com on implemening the ArrayAccess interface, I get some unrelated error.
Maybe it's a documentation bug again...
Reproduce code:
---------------
<?php
class Article implements ArrayAccess {
public $title;
function __construct($title) {
$this->title = $title;
}
function offsetSet($key, $value){
$this->{$key} = &$value;
}
function offsetGet($key){
return $this->{$key};
}
function offsetUnset($key){
unset($this->{$key});
}
function offsetExists($offset) {
return array_key_exists($offset,get_object_vars($this));
}
}
// Create the object
$A = new Article('SPL Rocks');
// Change the title using array syntax using reference
$str = 'SPL _really_ rocks';
$A['title'] =&$str;
// Check what it looks like again
print_r($A);
?>
Expected result:
----------------
Article Object
(
[title] => SPL _really_ rocks
)
Actual result:
--------------
Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference in test.php on line 27
There was no increment/decrement operations used.
It would be _much_ better if it would be allowed to write
function offsetSet($key, &$value){...}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 20:00:01 2025 UTC |
P.S. I am not allowed to define function offsetGet($key)as "function &offsetGet($key){...}" to return the value by reference due to the requirements of the ArrayAccess interface.