オブジェクトがこれを行うことを許可する PHP SPL インターフェイスは次のうちどれですか。
$object->month = 'january';
echo $object['month']; // january
$record['day'] = 'saturday';
echo $record->day; // saturday
例えば、Doctrine (Doctrine_Record) のようなライブラリのように
どうすればこれを実装できますか? ArrayObject を使用してみましたが、思ったように動作しません。
すなわち
$object = new ArrayObject();
$object['a'] = 'test';
$object['a'] == $object->a; // false
編集:
Arrayable と呼ばれるベアボーン実装を試しました。
class Arrayable implements ArrayAccess
{
protected $container = array();
# implement ArrayAccess methods to allow array notation
# $object = new Arrayable();
# $object['value'] = 'some data';
function offsetExists($offset)
{
return isset($this->container[$offset]);
}
function offsetGet($offset)
{
return $this->container[$offset];
}
function offsetSet($offset, $value)
{
$this->container[$offset] = $value;
}
function offsetUnset($offset)
{
unset($this->container[$offset]);
}
# now, force $object->value to map to $object['value']
# using magic methods
function __set($offset, $value)
{
$this->offsetSet($offset, $value);
}
function __get($offset)
{
return $this->offsetGet($offset);
}
}