php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38118 unrelated error when using reference
Submitted: 2006-07-16 23:37 UTC Modified: 2021-08-18 14:55 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: public at grik dot net Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.2.0 OS: *
Private report: No CVE-ID: None
 [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){...}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-16 23:43 UTC] 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:55 UTC] 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-17 00:42 UTC] public at grik dot net
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
 [2006-07-17 06:36 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.2-win32-latest.zip


 [2006-07-24 00:00 UTC] public at grik dot net
exactly the same error

[root@demo www]# php -v
PHP 5.2.0-dev (cli) (built: Jul 24 2006 02:54:55)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies
[root@demo www]# php test.php

Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference in /www/test.php on line 27
 [2016-03-20 13:06 UTC] nikic@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2016-03-20 13:06 UTC] nikic@php.net
The error message has been changed to refer to "indirect modification" in 5.2.1, see https://3v4l.org/upViG.

Nowadays it is also possible to return by reference from offsetGet, in which case this code is allowed: https://3v4l.org/muTTS

However, the assignment has no reference-breaking semantics.
 [2016-03-20 18:09 UTC] public at grik dot net
круто, что вспомнили :)
 [2021-08-18 14:55 UTC] cmb@php.net
-Package: *General Issues +Package: Scripting Engine problem
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC