What are the advantages of using SplFixedArray instead of an array?

4

What are the advantages of using SplFixedArray instead of an array?

What are the advantages of setting a value for an array (in PHP)?

Example with array :

$arr = array();

$arr[0] = 'Stack';

$arr[1] = 'Overflow';

$arr[2] = 'blablabla';

Example with SplFixedArray

$arr = new SplFixedArray(2);

$arr[0] = 'Stack';

$arr[1] = 'Overflow';

$arr[2] = 'blablabla'; // RunTimeException: Index invalid or out of range 
    
asked by anonymous 10.07.2015 / 13:55

1 answer

2

The advantage is that it allows quick implementation of Array . As illustrated by the benchmarks made by the author of this article :

In addition, the memory consumption of SplFixedArray is actually smaller, but noticeable only for a large amount of array elements. Because SplFixedArray is technically an instance of a class, unlike traditional arrays.

    
15.07.2015 / 19:03