PHP: SplFixedArray

SplFixedArray is a data structure provided by the Standard PHP Library(SPL). Works similar to the array but is memory efficient. So when memory consumption is a concern in some part of your PHP application then use SplFixedArray instead of standard array.

SplFixedArray class provides a fixed size array. It can be resized, but has to resized manually.


SplFixedArray class is part of the Standard PHP Library(SPL) and it comes with the PHP core (so no extra installation is necessary to use this).
Though memory consumption is the case in SplFixedArray, but the execution time can be higher for some operations(for a large size of data).

Benefits of SplFixedArray over Array

What are the benefits of using SplFixedArray instead of a normal array in PHP?

By implementing one or more interfaces we ensure that the class has all the methods declared in the interface(s).
We can use the interface as the type of a method argument or object variable, and ensure that the argument/variable has the method declared in the interface.

SplFixedArray Capability/Rules

SplFixedArray can do

Has a fixed size.
Index must be an integer.

SplFixedArray can not do

Interface Declaration

We can declare an interface by using the keyword “interface”.

Following is a valid interface declaration-


interface BigBoxInterface {

Add Method Declaration to Interface

All methods declared in an interface must be public.

Add function declaration to interface-


function xrange(int $start, int $end, int $step = 1) {
    $chunkSize = 100;
    $indexCounter = 0;
    $data = new SplFixedArray($chunkSize);

    for($i = $start; $i <= $end; $i++) {

        $data[$indexCounter++] = $i;

        if ($indexCounter === $chunkSize) {
            yield $data;

            $data = new SplFixedArray($chunkSize);
            $indexCounter = 0;

    if ($data->count()) {
        yield $data;

// Demo
foreach(xrange(1, 956) as $chunk) {


If the method is not declared public then we get a Fatal Error with the message-

PHP Fatal error:  Access type for interface method 
BigBoxInterface::myFunc() must be public 
in test.php on line 4

Leave a Comment

The reCAPTCHA verification period has expired. Please reload the page.