Edit report at https://bugs.php.net/bug.php?id=55293&edit=1
ID: 55293 User updated by: RQuadling at GMail dot com Reported by: RQuadling at GMail dot com Summary: ArrayObject doesn't pass use offsetSet() Status: Open Type: Bug Package: Arrays related Operating System: Windows XP SP3 PHP Version: 5.3.7RC3 Block user comment: N Private report: N New Comment: OOI, I'm not really filtering on scalar types, but on interfaces. Previous Comments: ------------------------------------------------------------------------ [2011-07-27 12:09:34] RQuadling at GMail dot com Description: ------------ I while ago, I created a TypedArray class that restricted its contents to specific types by overriding the ArrayObject::offsetSet() method and applying type checking to the value being supplied. This works fine if you create the arrayobject first and use conventional array appending code. Today, I supplied the data as part of the constructor, had made a mistake in the type I wanted to restrict things to and no filtering of the type was applied. I realised that calling the constructor for ArrayObject with data, doesn't pass the data through offsetSet(), so the override/filtering never took place. Is this a bug? The solution is to manually parse the array in the constructor. Example below. Test script: --------------- <?php class NoNumbersV1 extends ArrayObject { public function offsetSet($i_Offset, $m_Value) { if (!is_numeric($m_Value)) { parent::offsetSet($i_Offset, $m_Value); } } } class NoNumbersV2 extends ArrayObject { public function __construct($input = array(), $flags = 0, $iteratorClass = 'ArrayIterator') { parent::__construct(array(), $flags, $iteratorClass); foreach($input as $m_Key => $m_Value) { $this[$m_Key] = $m_Value; } } public function offsetSet($i_Offset, $m_Value) { if (!is_numeric($m_Value)) { parent::offsetSet($i_Offset, $m_Value); } } } $StringsV1 = new NoNumbersV1(array('One', 1, 'Two', 2, 'Three', 3)); $StringsV1[] = 'Four'; $StringsV1[] = 4; $StringsV2 = new NoNumbersV2(array('One', 1, 'Two', 2, 'Three', 3)); $StringsV2[] = 'Four'; $StringsV2[] = 4; print_r($StringsV1); print_r($StringsV2); Expected result: ---------------- NoNumbersV1 Object ( [storage:ArrayObject:private] => Array ( [0] => One [2] => Two [4] => Three [5] => Four ) ) NoNumbersV2 Object ( [storage:ArrayObject:private] => Array ( [0] => One [2] => Two [4] => Three [5] => Four ) ) Actual result: -------------- NoNumbersV1 Object ( [storage:ArrayObject:private] => Array ( [0] => One [1] => 1 [2] => Two [3] => 2 [4] => Three [5] => 3 [6] => Four ) ) NoNumbersV2 Object ( [storage:ArrayObject:private] => Array ( [0] => One [2] => Two [4] => Three [5] => Four ) ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=55293&edit=1