php.net |  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
Votes:1
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
 [2013-06-25 08:04 UTC] knight at kopernet dot org
Description:
------------
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.


Patches

Pull Requests

History

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: <https://phpunit.readthedocs.io/en/8.3/test-doubles.html>. (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: <https://github.com/dg/bypass-finals>

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):

https://lists.wikimedia.org/pipermail/wikitech-l/2019-August/092432.html
https://phabricator.wikimedia.org/T231419
 [2019-10-29 15:02 UTC] requinix@php.net
-Status: Open +Status: Wont fix
 [2019-10-29 15:02 UTC] requinix@php.net
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-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 22 00:01:30 2024 UTC