The SplFixedArray class
SplFixedArray
&reftitle.intro;
The SplFixedArray class provides the main functionalities of array. The
main difference between a SplFixedArray and a normal PHP array is that
the SplFixedArray must be resized manually and allows only integers within
the range as indexes. The advantage is that it uses less memory than
a standard array.
&reftitle.classsynopsis;
SplFixedArray
implements
IteratorAggregate
ArrayAccess
Countable
JsonSerializable
&Methods;
&reftitle.changelog;
&Version;
&Description;
8.2.0
The SplFixedArray::__serialize and
SplFixedArray::__unserialize
magic methods have been added to SplFixedArray.
8.1.0
SplFixedArray implements
JsonSerializable now.
8.0.0
SplFixedArray implements
IteratorAggregate now.
Previously, Iterator was implemented instead.
&reftitle.examples;
SplFixedArray usage example
setSize(10);
$array[9] = "asdf";
// Shrink the array to a size of 2
$array->setSize(2);
// The following lines throw a RuntimeException: Index invalid or out of range
try {
var_dump($array["non-numeric"]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
try {
var_dump($array[-1]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
try {
var_dump($array[5]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
?>
]]>
&example.outputs;
&reference.spl.entities.splfixedarray;