Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Feature request for array cast implementation. See https://bugs.php.net/bug.php?id=72579 for details.
<?php
class ItemList implements Iterator
{
protected $items = ['foo' => 'bar', 'baz' => 123];
public function current()
{
return current($this->items);
}
public function next()
{
return next($this->items);
}
public function key()
{
return key($this->items);
}
public function valid()
{
return $this->key() !== null;
}
public function rewind()
{
reset($this->items);
}
}
echo "Actual result:\n";
var_dump((array)(new ItemList()));
echo "\nExpected result:\n";
$tmp = []; // Everything from here up to line 39 should be done by PHP internally when casting (implicitly or explicitly) to an array
foreach(new ItemList() as $k => $v) {
$tmp[$k] = $v;
}
var_dump($tmp);
/* Output:
Actual result:
array_cast_example.php:34:
array(1) {
'\0*\0items' =>
array(2) {
'foo' =>
string(3) "bar"
'baz' =>
int(123)
}
}
Expected result:
array_cast_example.php:40:
array(2) {
'foo' =>
string(3) "bar"
'baz' =>
int(123)
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment