Re: Class Re-implementation Mechanism

From: Date: Tue, 21 Feb 2023 17:59:30 +0000
Subject: Re: Class Re-implementation Mechanism
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-119591@lists.php.net to get a copy of this message
On Tue, Feb 21, 2023 at 7:53 AM someniatko <someniatko@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).

« previous php.internals (#119591) next »