Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Question Fatal error: Cannot assign by reference to overloaded object?

    hi peoples,

    anyone know why this throws a fatal error?

    PHP Code:
    class MyArrayObject extends ArrayObject {};
    $obj = new MyArrayObject;
    $moo 'moo';
    $obj['myVar'] = &$moo 
    Code:
    Fatal error: Cannot assign by reference to overloaded object in...
    but this works fine
    PHP Code:
    class MyArrayObject extends ArrayObject {};
    $moo 'moo';
    $obj = new MyArrayObject(array('myVar' => &$moo)); 
    thanks,
    leon
    Last edited by Leeoniya; 06-08-2011 at 05:05 PM.
    "I only know that I know nothing."
    -Socrates

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    17,025
    Thanks
    4
    Thanked 2,668 Times in 2,637 Posts
    That would be the ArrayAccess offsetSet method. ArrayAccess does not slate any returned by reference or parameter by reference methods.
    5.3.4 appears to have fixed the issues with the byref, so you may use version 5.3.4 or higher. It appears they have simply relaxed the prototype to it so that you can override with a slightly different signature in order to force the references.

  3. Users who have thanked Fou-Lu for this post:

    Leeoniya (06-08-2011)

  4. #3
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    nice digging, thanks!
    "I only know that I know nothing."
    -Socrates

  5. #4
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    well, i updated to 5.3.6 and not much luck.

    here's what i have now

    Code:
    class MyArrayObject extends ArrayObject
    {
    	public function &offsetGet($key) {
    		return parent::offsetGet($key);
    	}
    }
    Code:
    Notice: Indirect modification of overloaded element of MyArrayObject has no effect in...
    
    Fatal error: Cannot assign by reference to overloaded object in...
    should i not be using "parent::offsetGet"? i think there is a private "$storage" variable that holds the actual array in the parent, can i somehow use that directly?
    "I only know that I know nothing."
    -Socrates

  6. #5
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    "I only know that I know nothing."
    -Socrates

  7. #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    17,025
    Thanks
    4
    Thanked 2,668 Times in 2,637 Posts
    I haven't seen anything quite yet in regards to the ArrayObject whether it were fixed, just the ArrayAccess. Its possible that the interface alone has been relaxed, but since classes themselves are not this may not work as expected.
    Try constructing a simple Vector or LinkedList class manually, and apply the ArrayAccess to it allowing both byref return and byref parameters. If it works, that would indicate that ArrayObject still cannot be overridden. There is a suggestion made to return an ArrayObject itself by one of the PHP team members, but I'm afraid I didn't read it with the remaining context.

  8. #7
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    this is for the internal storage of a linq-ish lib i plan to open-source, so i can't reasonably require 5.3.4+

    looks like i'll just use a plain array internally and implement ArrayAccess + IteratorAggregate on the container with a custom iterator as a work-around.
    "I only know that I know nothing."
    -Socrates


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •