#38118 [Bgs->Opn]: unrelated error when using reference
| From: | public at grik dot net | Date: | Mon, 17 Jul 2006 00:42:48 +0000 |
| Subject: | #38118 [Bgs->Opn]: unrelated error when using reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-99486@lists.php.net to get a copy of this message | ||
ID: 38118
User updated by: public at grik dot net
Reported By: public at grik dot net
-Status: Bogus
+Status: Open
-Bug Type: Scripting Engine problem
+Bug Type: Feature/Change Request
Operating System: *
PHP Version: 5.1.4
New Comment:
Thank you.
The problem of using reference in ArrayAccess class is pure practical.
I have a set of rules and data fields - each field should be checked
against it's rule (e.g. min, max).
I write the hierarchy of classes implementing the exact logic for each
rule. The class instance should get the set of rule parameters and the
data reference.
Then I interate over the management object (extends ArrayObject) with
foreach and get the checking object as a value.
As result, I can make the rules to check the data regardless of it's
type and source - scalar, array, resource. All I need is to write
another class with the check logic.
The data may be large, and inability to use references may lead either
to
- memory overconsumption
- DDOS-insecure issue
- having to refuse from abstration and write more code
Previous Comments:
------------------------------------------------------------------------
[2006-07-16 23:55:43] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
The '&' is because of inhereitance rules.
The second thing you encounter, the '&=' is an engine limitation.
Btw, both are pure engine issues, nothing todo with SPL.
Maybe you want this reopened as feature request. But then i suggest you
provide at least some ideas where and how to document that ArayAccess
cannot be used in modifying operators like '&='.
Either way you should try 5.2/HEAD first as we've put some work to get
you a better error message.
------------------------------------------------------------------------
[2006-07-16 23:43:09] public at grik dot net
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.
------------------------------------------------------------------------
[2006-07-16 23:37:34] 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){...}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=38118&edit=1