[DOC-CVS] [doc-en] master: Fix SplFixedArray example for non-integer key error type (#5227)
[email protected] (Louis-Arnaud via GitHub)
| Newsgroups | php.doc.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: Louis-Arnaud (lacatoire)
Committer: GitHub (web-flow)
Pusher: lacatoire
Date: 2026-08-26T09:55:04+02:00
Commit: https://github.com/php/doc-en/commit/a833060594749e44ff296fee01fd587d6395cff7
Raw diff: https://github.com/php/doc-en/commit/a833060594749e44ff296fee01fd587d6395cff7.diff
Fix SplFixedArray example for non-integer key error type (#5227)
Since PHP 8.1.0, using a non-integer key throws TypeError instead of
RuntimeException. Updated the example to correctly catch TypeError
and document this behavior change.
Fixes https://github.com/php/doc-en/issues/3679
Changed paths:
M reference/spl/splfixedarray.xml
Diff:
diff --git a/reference/spl/splfixedarray.xml b/reference/spl/splfixedarray.xml
index 60a530977a01..2f9cbb67a46e 100644
--- a/reference/spl/splfixedarray.xml
+++ b/reference/spl/splfixedarray.xml
@@ -82,6 +82,14 @@
magic methods have been added to <classname>SplFixedArray</classname>.
</entry>
</row>
+ <row>
+ <entry>8.1.0</entry>
+ <entry>
+ Using a non-integer key on a <classname>SplFixedArray</classname> now throws
+ a <exceptionname>TypeError</exceptionname> instead of a
+ <exceptionname>RuntimeException</exceptionname>.
+ </entry>
+ </row>
<row>
<entry>8.1.0</entry>
<entry>
@@ -130,23 +138,25 @@ $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
+// The following two blocks throw a RuntimeException: Index invalid or out of range
try {
- var_dump($array["non-numeric"]);
-} catch(RuntimeException $re) {
- echo "RuntimeException: ".$re->getMessage()."\n";
+ var_dump($array[-1]);
+} catch(RuntimeException $e) {
+ echo "RuntimeException: ".$e->getMessage()."\n";
}
try {
- var_dump($array[-1]);
-} catch(RuntimeException $re) {
- echo "RuntimeException: ".$re->getMessage()."\n";
+ var_dump($array[5]);
+} catch(RuntimeException $e) {
+ echo "RuntimeException: ".$e->getMessage()."\n";
}
+// As of PHP 8.1.0, using a non-integer key throws a TypeError.
+// Prior to PHP 8.1.0, a RuntimeException was thrown instead.
try {
- var_dump($array[5]);
-} catch(RuntimeException $re) {
- echo "RuntimeException: ".$re->getMessage()."\n";
+ var_dump($array["non-numeric"]);
+} catch(TypeError $e) {
+ echo "TypeError: ".$e->getMessage()."\n";
}
?>
]]>
@@ -159,11 +169,18 @@ int(2)
string(3) "foo"
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range
-RuntimeException: Index invalid or out of range
+TypeError: Illegal offset type
]]>
</screen>
</example>
</para>
+ <note>
+ <simpara>
+ Prior to PHP 8.1.0, accessing a <classname>SplFixedArray</classname> with a
+ non-integer key threw a <exceptionname>RuntimeException</exceptionname>
+ instead of a <exceptionname>TypeError</exceptionname>.
+ </simpara>
+ </note>
</section>
<!-- }}} -->