ArrayObject class
This class as well as its derivatives allows objects to work as arrays.
<?php
ArrayObject implements IteratorAggregate , ArrayAccess , Serializable , Countable {
/* Constants */
const integer STD_PROP_LIST = 1 ;
const integer ARRAY_AS_PROPS = 2 ;
/* Methods */
public __construct ([ mixed $input = array() [, int $flags = 0 [, string $iterator_class = "ArrayIterator" ]]] )
public append ( mixed $value ) : void
public asort ( void ) : void
public count ( void ) : int
public exchangeArray ( mixed $input ) : array
public getArrayCopy ( void ) : array
public getFlags ( void ) : int
public getIterator ( void ) : ArrayIterator
public getIteratorClass ( void ) : string
public ksort ( void ) : void
public natcasesort ( void ) : void
public natsort ( void ) : void
public offsetExists ( mixed $index ) : bool
public offsetGet ( mixed $index ) : mixed
public offsetSet ( mixed $index , mixed $newval ) : void
public offsetUnset ( mixed $index ) : void
public serialize ( void ) : string
public setFlags ( int $flags ) : void
public setIteratorClass ( string $iterator_class ) : void
public uasort ( callable $cmp_function ) : void
public uksort ( callable $cmp_function ) : void
public unserialize ( string $serialized ) : void
}
?>
ArrayObject::STD_PROP_LIST
Properties of the object have their normal functionality when accessed as list.
ArrayObject::ARRAY_AS_PROPS
Entries can be accessed as properties.
IteratorAggregate extends Traversable
Internal interface to detect if a class is traversable using foreach and cannot be implemented alone.
Instead it must be implemented by either IteratorAggregate or Iterator.
Associated with construct do not need to implement IteratorAggregate or Iterator.
<?php
IteratorAggregate extends Traversable {
/* Methods */
abstract public getIterator ( void ) : Traversable
}
?>
<?php
Iterator extends Traversable {
/* Methods */
abstract public current ( void ) : mixed
abstract public key ( void ) : scalar
abstract public next ( void ) : void
abstract public rewind ( void ) : void
abstract public valid ( void ) : bool
}
?>
<?php
ArrayAccess {
/* Methods */
abstract public offsetExists ( mixed $offset ) : bool
abstract public offsetGet ( mixed $offset ) : mixed
abstract public offsetSet ( mixed $offset , mixed $value ) : void
abstract public offsetUnset ( mixed $offset ) : void
}
?>
<?php
Serializable {
/* Methods */
abstract public serialize ( void ) : string
abstract public unserialize ( string $serialized ) : void
}
?>
<?php
Countable {
/* Methods */
abstract public count ( void ) : int
}
?>
ATTENTION
Accessed as a list means the use of the following functions: var_dump, print_r, foreach, etc. and accessed as properties: read and write.
EXERCISE
<?php
class Color {
public $propr = 'CMYK';
}
$arrcolor = new ArrayObject(new Color());
$arrcolor->offsetSet(1, 'cyan');
$arrcolor->offsetSet(2, 'magenta');
$arrcolor->offsetSet(3, 'yellow');
$arrcolor->offsetSet(4, 'black');
$arrcolor->offsetSet('RGB', ['red', 'green', 'blue']);
echo '<pre>';
var_dump($arrcolor);
echo '</pre>';
?>
RESULT
object(ArrayObject)#1 (1) {
["storage":"ArrayObject":private]=>
object(Color)#2 (6) {
["propr"]=>
string(4) "CMYK
[1]=>
string(4) "cyan"
[2]=>
string(7) "magenta"
[3]=>
string(6) "yellow"
[4]=>
string(5) "black"
["RGB"]=>
array(3) {
[0]=>
string(3) "red"
[1]=>
string(5) "green"
[2]=>
string(4) "blue"
}
}
}