ArrayAccess or what's the difference between NULL and nothing

So if you have class foo implements ArrayAccess, implement offsetSet simply as $this->context[$offset] = $value; then doing $x = new Foo; $x[]  = 1; results in context set to array('' => 1). You need to write this out:

<?php
if (isset($offset)) {
 
$this->context[$offset] = $value;
}
else {
 
$this->context[] = $value;
}
?>

You can try simply:
var_dump(array(NULL => 1));
array(1) {
  '' =>
  int(1)
}

For ArrayAccess, there is absolutely no way to discern between $a[NULL] = 1 and $a[] = 1. Using the code above answers for the more common $a[] = 1; case but it's not 100% equivalent of an array.

Now, ArrayObject does have an append() method but as a note on the handbook page notes it's implemented as offset(NULL, $value), completely missing the excellent opportunity to do something useful with it.

There's a bug open asking for an ArrayAccess::append() and I guess a saner ArrayObject::append but there doesn't seem to be anything happening there since 2009.

Comments

It's been a while!

Thought you'd fallen off the planet or something. How I've missed my PHP WTFs.