|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65119 provide a way to inherit a final class or overriding final methods
Submitted: 2013-06-25 08:04 UTC Modified: 2019-10-29 15:02 UTC
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: knight at kopernet dot org Assigned:
Status: Wont fix Package: Reflection related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-06-25 08:04 UTC] knight at kopernet dot org
Please provide a way to make possible to bypass the final keyword restriction in similar way the ReflectionAPI makes possible to call private or protected members.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-10-29 14:27 UTC] ayg at aryeh dot name
This causes a significant limitation in PHPUnit (and presumably other unit-testing frameworks). It is not possible to mock final classes or methods: <>. (Mocking a class subclasses it and overrides the methods to be mocked, which doesn't work if they're final.) This makes it difficult to write unit tests for code that uses final classes or methods.

Someone went so far as to write a library that strips "final" from all PHP code to work around this problem: <>

This feature would be useful for MediaWiki, see discussion (we actually want to try using that final-stripping library for test runs to work around the problem):
 [2019-10-29 15:02 UTC]
-Status: Open +Status: Wont fix
 [2019-10-29 15:02 UTC]
Reflection allows calling private and protected members because that can be done at runtime. Creating a new class/method that extends a final parent happens at compile-time; the only way to bypass it would be to modify the class data in memory before the child class is loaded, and that is more of a "runkit" operation than a reflection operation.

If you've written a lot of tests then you know that sometimes you have to refactor the source to make testing possible. This is one of those times.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Jul 06 22:05:44 2022 UTC