phpsplarrayobjectarrayiterator

What does the flags parameter of ArrayIterator and ArrayObject do?


The constructors of PHP's ArrayIterator and ArrayObject have a flags parameter that is documented in ArrayObject::setFlags() as follows:

ArrayObject::STD_PROP_LIST

Properties of the object have their normal functionality when accessed as list (var_dump, foreach, etc.).

ArrayObject::ARRAY_AS_PROPS

Entries can be accessed as properties (read and write).

The documentation of ArrayIterator::setFlags() is similar (the difference being difference erroneous).

Now, I understand the second flag quite well, it allows property access with -> as well as array access with []:

$a = new ArrayIterator(array('foo'=>1), ArrayObject::ARRAY_AS_PROPS);
var_dump($a['foo']);
var_dump($a->foo);
int(1)
int(1)

But I cannot wrap my head around ArrayObject::STD_PROP_LIST. As far as I tested it, var_dump and foreach behave exactly the same with or without this flag. What am I missing here?


Solution

  • As stated in the comments in the PHP manual the flag causes the properties, instead of the array values to be visible in var_dump.

    Credits for this go to the commenter in the PHP manual:

    <?php                                                    
    
    $a = new ArrayObject(array(), ArrayObject::STD_PROP_LIST);
        $a['arr'] = 'array data';                            
        $a->prop = 'prop data';                              
    $b = new ArrayObject();                                  
        $b['arr'] = 'array data';                            
        $b->prop = 'prop data';                              
    
    // ArrayObject Object                                    
    // (                                                     
    //      [prop] => prop data                              
    // )                                                     
    print_r($a);                                             
    
    // ArrayObject Object                                    
    // (                                                     
    //      [arr] => array data                              
    // )                                                     
    print_r($b);                                             
    
    ?>
    

    This behaviour probably changed within never versions of PHP, as my version (5.4.6) of PHP always shows both: Properties and array values:

    ArrayObject Object
    (
        [prop] => prop data
        [storage:ArrayObject:private] => Array
            (
                [arr] => array data
            )
    
    )
    ArrayObject Object
    (
        [prop] => prop data
        [storage:ArrayObject:private] => Array
            (
                [arr] => array data
            )
    
    )