splfixedarray
SplFixedArray implements IteratorAggregate, ArrayAccess, Countable, JsonSerializable
A fixed-length array: its size is set at construction (changed manually via setSize), it accepts only integer keys, uninitialized slots are NULL, and it is memory-compact — a fundamental contrast to the regular PHP array (auto-growing, mixed keys, hash-bucket overhead); out-of-bounds access throws OutOfBoundsException.
C C23
#include <stdio.h>
#include <stdlib.h>
int main(void) {
size_t n = 3;
char **f = malloc(n * sizeof(char *)); /* a fixed block of 3 slots */
f[0] = "red";
f[1] = "green";
f[2] = "blue";
printf("%zu\n", n); /* 3 */
printf("%s\n", f[1]); /* green */
/* no bounds checking: f[3] would silently corrupt memory — the classic C trap */
f = realloc(f, 4 * sizeof(char *)); /* "resize": move to a bigger block */
f[3] = NULL;
printf("%zu\n", n + 1); /* 4 */
printf("%d\n", f[3] == NULL); /* fresh slot is NULL */
free(f);
return 0;
}
3 green 4 1
C++ C++26
#include <algorithm>
#include <array>
#include <iostream>
#include <string>
int main() {
std::array<std::string, 3> f; // length is part of the type, fixed at compile time
f[0] = "red";
f[1] = "green";
f[2] = "blue";
std::cout << f.size() << "\n"; // 3
std::cout << f[1] << "\n"; // green
// f[3] = "x"; // compile error: index out of bounds (at() throws instead)
// "resizing" = copy into a bigger array
std::array<std::string, 4> g;
std::copy(f.begin(), f.end(), g.begin());
std::cout << g.size() << "\n"; // 4
std::cout << g[3].empty() << "\n"; // fresh slot defaults to ""
return 0;
}
3 green 4 1
C# v11.0.100
using System;
var f = new string[3]; // length is fixed at creation
f[0] = "red";
f[1] = "green";
f[2] = "blue";
Console.WriteLine(f.Length); // 3
Console.WriteLine(f[1]); // green
try {
f[3] = "x";
} catch (IndexOutOfRangeException) {
Console.WriteLine("IndexOutOfRangeException");
}
// "resizing" = allocate a new array and copy
Array.Resize(ref f, 4);
Console.WriteLine(f.Length); // 4
Console.WriteLine(f[3] == null); // fresh slot defaults to null
3 green IndexOutOfRangeException 4 True
Go v1.27.4
package main
import "fmt"
func main() {
var f [3]string // array: length is part of the type, fixed at compile time
f[0] = "red"
f[1] = "green"
f[2] = "blue"
fmt.Println(len(f)) // 3
fmt.Println(f[1]) // green
// f[3] = "x" // compile error: index 3 out of bounds (caught by the compiler!)
// arrays are values: assignment copies the whole thing
g := f
g[0] = "RED"
fmt.Println(f[0], g[0]) // red RED
// slices are the dynamic counterpart: make + append
s := make([]string, 3)
copy(s, f[:])
s = append(s, "gold")
fmt.Println(len(s)) // 4
fmt.Println(s[3]) // gold
}
3 green red RED 4 gold
Java v27.0.1
public class Main {
public static void main(String[] args) {
String[] f = new String[3]; // length is fixed at creation
f[0] = "red";
f[1] = "green";
f[2] = "blue";
System.out.println(f.length); // 3
System.out.println(f[1]); // green
try {
f[3] = "x";
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException");
}
// "resizing" = allocate a new array and copy
String[] g = java.util.Arrays.copyOf(f, 4);
System.out.println(g.length); // 4
System.out.println(g[3] == null); // fresh slot defaults to null
}
}
3 green ArrayIndexOutOfBoundsException 4 true
JavaScript v26.13.1
// JS Array is dynamic; fixed-length flavor comes from TypedArray
const f = ["red", "green", "blue"];
console.log(f.length); // 3
console.log(f[1]); // green
f.push("gold"); // arrays grow freely
console.log(f.length); // 4
console.log(f[3]); // gold
console.log(f[9]); // undefined: out-of-range read never throws
// TypedArray is the fixed-length, index-checked flavor
const t = new Uint8Array(3); // three bytes, zero-filled, cannot grow
t[0] = 65;
console.log(t.length); // 3
console.log(t[0]); // 65
3 green 4 gold undefined 3 65
PHP v8.6.0
$f = new SplFixedArray(3);
$f[0] = "red";
$f[1] = "green";
$f[2] = "blue";
echo $f->getSize(), "\n"; // 3
echo $f[1], "\n"; // green
$f->setSize(4); // grow: new slot defaults to NULL
echo $f->getSize(), "\n"; // 4
var_dump($f[3]); // NULL
try {
$f[4] = "x";
} catch (OutOfBoundsException $e) {
echo "OutOfBoundsException: ", $e->getMessage(), "\n";
}
try {
$f["name"] = "x";
} catch (TypeError $e) {
echo "TypeError: string keys not allowed\n";
}
3 green 4 NULL OutOfBoundsException: Index invalid or out of range TypeError: string keys not allowed
Python v3.15.2
# Python's core has no fixed-length array; list is dynamic and index-checked
f = ["red", "green", "blue"]
print(len(f)) # 3
print(f[1]) # green
f.append("gold") # lists grow freely
print(len(f)) # 4
print(f[3]) # gold
try:
f[4] = "x"
except IndexError:
print("IndexError") # writing past the end still fails
# array.array gives compact typed storage, but length is still dynamic
import array
a = array.array("i", [10, 20, 30])
print(len(a)) # 3
3 green 4 gold IndexError 3
Rust v1.99.1
fn main() {
// fixed length is part of the type; out-of-range is a compile error
let mut f: [Option<&str>; 3] = [None; 3];
f[0] = Some("red");
f[1] = Some("green");
f[2] = Some("blue");
println!("{}", f.len()); // 3
println!("{}", f[1].unwrap()); // green
// f[3] = Some("x"); // compile error: index out of bounds
// "resizing" = move into a bigger array
let mut g: [Option<&str>; 4] = [None; 4];
g[..3].copy_from_slice(&f);
println!("{}", g.len()); // 4
println!("{}", g[3].is_none()); // fresh slot is None
// Vec is the dynamic counterpart
let mut v: Vec<&str> = vec!["red", "green", "blue"];
v.push("gold");
println!("{}", v.len()); // 4
}
3 green 4 true 4
TypeScript v26.13.1
// TS Array is dynamic; fixed-length flavor is the tuple type
const f: string[] = ["red", "green", "blue"];
console.log(f.length); // 3
console.log(f[1]); // green
f.push("gold");
console.log(f.length); // 4
console.log(f[3]); // gold
console.log(f[9]); // undefined: no out-of-range error by default
// tuple: fixed length enforced by the type checker
const t: [string, string, string] = ["red", "green", "blue"];
console.log(t.length); // 3
3 green 4 gold undefined 3
Language differences
C: fixed length is one malloc'd block — with no bounds checking; f[3] silently corrupts memory (the classic C trap); "resizing" means realloc to a bigger block. SplFixedArray can be read as "a malloc block with bounds checking and integer keys".
C++: std::array<T, N> bakes the length into the type, so out-of-range indexing is a compile error (operator[] skips runtime checks; only at() throws out_of_range).
C#: arrays are fixed-length; f[3] throws IndexOutOfRangeException; Array.Resize actually allocates a new array and copies (the new reference comes back through the ref parameter).
Go: an array's length is part of its type ([3]string), so out-of-range is a compile error; in practice slices (dynamic) are used, and array assignment copies the whole value — the demo's g := f keeps g's edits away from f.
Java: arrays are fixed-length; runtime out-of-range throws ArrayIndexOutOfBoundsException; growing means Arrays.copyOf.
JavaScript: Array is inherently dynamic — out-of-range reads yield undefined without throwing; the fixed-length flavor lives in TypedArray (fixed byte count, though out-of-range writes are silently ignored — a different trap).
PHP: the essential differences between SplFixedArray and a regular array: the length is fixed at construction (changed manually with setSize), only integer keys are accepted (string keys throw TypeError, since 8.1), uninitialized slots are NULL, and memory is compact (manual and user benchmarks: roughly three-quarters less memory at the million-element scale). Out-of-bounds access throws OutOfBoundsException (since 8.4; previously a RuntimeException with the same message, to which it is still assignable). The trade-off: most array_* functions do not apply, and [] appends are not possible.
Python: no native fixed-length array in the core — list is dynamic and raises IndexError out of range; array.array gives compact typed storage but the length stays dynamic.
Rust: out-of-range on a fixed array [T; N] is a compile error; the demo stores Option slots so fresh slots default to None; the dynamic counterpart is Vec::push.
TypeScript: the fixed-length flavor lives in the type layer — the tuple [string, string, string] enforces length at compile time but not at runtime (noUncheckedIndexedAccess can tighten out-of-range reads).