On Tue, Feb 21, 2023 at 7:53 AM someniatko <somenia...@gmail.com> wrote:

> We want to write some tests for the Service class, but we don't want
> to use a real SomeDependency instance
> during tests. A common approach is to either extract an interface
> (JUST to make it testable), or to drop the
> `final` keyword and allow extending the class.
>
> Both approaches have their flaws:
>  - extracting an interface unnecessarily complicates the system, where
> only one "real" implementation of an interface is assumed.
>  - dropping the `final` keyword allows for the rabbit-hole of
> inheritance abuse, like greatly described in this article:
> https://front-line-php.com/object-oriented

There's another approach that requires neither: removing `final`
during class loading, for tests only.

See https://github.com/dg/bypass-finals (and
https://github.com/nunomaduro/mock-final-classes for plug'n'play
integration with PHPUnit).

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to