How to override any class from vendor with Silverstripe

Silverstripe Version:
4.9.0

Question:
How can i override any class from vendor if i want to make some custom changes?
for example i want to add some input or change casting in SiteTree for Title

i need for example change this middleware

vendor/silverstripe/framework/src/Core/Config/Middleware/InheritanceMiddleware.php

There are a couple of main ways to override functionality, depending on the exact case. The usual approach would be to use an extension (https://docs.silverstripe.org/en/4/developer_guides/extending/extensions/#extensions-and-dataextensions) this allows you to add functionality to a class, and, if the class has extension points (generally identifiable in the code with the pattern $this->extend('somemethodname', $someVariables)), to override existing functionality.

If that’s not sufficient, then it’s possible to actually replace a class with your own custom version using Injector (https://docs.silverstripe.org/en/4/developer_guides/extending/injector/) Obviously that’s a more involved approach, and you need to ensure that you don’t introduce any regressions due to the class being used in places you didn’t anticipate (or if the base class is updated for some reason, and your replacement isn’t)