php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49454 Call ArrayObject::getArrayCopy() when casting an ArrayObject to an array
Submitted: 2009-09-03 11:55 UTC Modified: 2020-11-18 13:03 UTC
Votes:3
Avg. Score:3.3 ± 0.5
Reproduced:0 of 1 (0.0%)
From: info at adaniels dot nl Assigned:
Status: Suspended Package: SPL related
PHP Version: * OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: info at adaniels dot nl
New email:
PHP Version: OS:

 

 [2009-09-03 11:55 UTC] info at adaniels dot nl
Description:
------------
It would be nice if you could affect the behaviour of casting an ArrayObject to an array by overloading the getArrayCopy() method.

This would be usefull to create lazy load arrays.

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

class Items extends ArrayObject
{
	/**
	 * Stub for loading an item.
	 * @return object
	 */
	public function load($id)
	{
		return (object)(array('id'=>$id));
	}

	public function offsetGet($key)
	{
		$item = parent::offsetGet($key);
		if (!is_object($item)) {
			$item = $this->load($item);
			$this->offsetSet($key, $item);
		}
		return $item;
	}
	
	public function getArrayCopy()
	{
		$array = parent::getArrayCopy();
		foreach ($array as $i=>&$item) {
			if (!is_object($item)) {
				$item = $this->load($item);
				$this->offsetSet($i, $item);
			}
		}
		return $array;
	}
}

$a = new Items();
$a[] = 'jan';
$a[] = 'piet';

var_dump($a[0]);
var_dump((array)$a);

Expected result:
----------------
object(stdClass)#2 (1) {
  ["id"]=>
  string(3) "jan"
}
array(2) {
  [0]=>
  object(stdClass)#2 (1) {
    ["id"]=>
    string(3) "jan"
  }
  [1]=>
  object(stdClass)#3 (1) {
    ["id"]=>
    string(4) "piet"
  }
}


Actual result:
--------------
object(stdClass)#2 (1) {
  ["id"]=>
  string(3) "jan"
}
array(2) {
  [0]=>
  object(stdClass)#2 (1) {
    ["id"]=>
    string(3) "jan"
  }
  [1]=>
  string(4) "piet"
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-29 13:19 UTC] jani@php.net
-Package: Feature/Change Request +Package: SPL related -Operating System: Linux / Ubuntu 9.04 +Operating System: * -PHP Version: 5.3.0 +PHP Version: *
 [2020-11-18 13:03 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-11-18 13:03 UTC] cmb@php.net
This feature would need to be discussed, for what this bug tracker
is not suitable.  If you, or anybody else, is still interested in
this, please forward the request to the internals mailing list.
For the time being, I'm suspending this ticket.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 10:01:29 2024 UTC