From: Operating system: Windows XP SP3 PHP version: 5.3.7RC3 Package: Arrays related Bug Type: Bug Bug description:ArrayObject doesn't pass use offsetSet()
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 bug report at https://bugs.php.net/bug.php?id=55293&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=55293&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=55293&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=55293&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=55293&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=55293&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=55293&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=55293&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=55293&r=needscript Try newer version: https://bugs.php.net/fix.php?id=55293&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=55293&r=support Expected behavior: https://bugs.php.net/fix.php?id=55293&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=55293&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=55293&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=55293&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=55293&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=55293&r=dst IIS Stability: https://bugs.php.net/fix.php?id=55293&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=55293&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=55293&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=55293&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=55293&r=mysqlcfg