[ create a new paste ] login | about

Link: http://codepad.org/egAchYpk    [ raw code | output | fork ]

PHP, pasted on Mar 9:
<?php

class ColumnCollection extends ArrayIterator 
{
    public function offsetSet($index, $newValue)
    {
        if ($index === null && $newValue instanceof Column) {
            return parent::offsetSet($newValue->name, $newValue);
        }
        return parent::offsetSet($index, $newValue);
    }
}

class Column
{
    public $name = 'foo';
}

$collection = new ColumnCollection;
$collection[] = new Column;

print_r($collection->getArrayCopy());
var_dump($collection['foo']);


Output:
1
2
3
4
5
6
7
8
9
10
11
12
Array
(
    [foo] => Column Object
        (
            [name] => foo
        )

)
object(Column)#2 (1) {
  ["name"]=>
  string(3) "foo"
}


Create a new paste based on this one


Comments: