Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong Example in SplFixedArray docs #3679

Open
abdelrahman-gado opened this issue Aug 24, 2024 · 4 comments
Open

Wrong Example in SplFixedArray docs #3679

abdelrahman-gado opened this issue Aug 24, 2024 · 4 comments
Labels
bug Documentation contains incorrect information

Comments

@abdelrahman-gado
Copy link
Contributor

From manual page: https://php.net/class.splfixedarray


In the example of SplFixedArray Example #1 SplFixedArray usage example, you state that the following code will give RuntimeException but what it is actually throws is TypeError. So, the catch part will never executed.

// 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";
}

image

@cmb69
Copy link
Member

cmb69 commented Aug 24, 2024

Oh, indeed there's no more RuntimeException as of PHP 8.1.0 (https://3v4l.org/sdIQd). That example needs to be fixed (but should somehow address the version dependent behavior).

@cmb69 cmb69 added the bug Documentation contains incorrect information label Aug 24, 2024
@nielsdos
Copy link
Member

Quick thinking: change RuntimeException to Throwable in the example code and that would work. (and change the echo statement)

@damianwadley
Copy link
Member

The documentation always favors the most recent versions - that's why there are changelog tables. So why not solve this the same way other versioning issues are solved: write it to be valid for 8.3 and add a code comment to the effect of "// throws RuntimeException in PHP 8.0 and earlier"?

Side note: speaking of changelog tables, the offsetGet/Set/Unset docs all say they throw RuntimeException too.

@cmb69
Copy link
Member

cmb69 commented Aug 24, 2024

Actually, I don't think we should tell people to catch TypeErrors (they should fix their code instead). Maybe we should move the var_dump($array["non-numeric"]); to the bottom, commenting that this is a TypeError (but prior to PHP 8.1.0 has been a RuntimeException).

Implementation nit: passing an out of range index still reports "Index invalid or out of range" (https://3v4l.org/GMIMC).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Documentation contains incorrect information
Projects
None yet
Development

No branches or pull requests

4 participants