I want to override a class from the JMS/serializer bundle. Unfortunately this class is marked "final" and I can’t override it. What would be the best method to circumvent this problem please?
I’m on symfony5.4
Here is the class I want to override :
<?php
declare(strict_types=1);
namespace JMSSerializerHandler;
use JMSSerializerGraphNavigatorInterface;
use JMSSerializerVisitorSerializationVisitorInterface;
use JMSSerializerXmlSerializationVisitor;
use SymfonyComponentFormForm;
use SymfonyComponentFormFormError;
use SymfonyComponentFormFormInterface;
use SymfonyComponentTranslationTranslatorInterface;
use SymfonyContractsTranslationTranslatorInterface as TranslatorContract;
use function get_class;
final class FormErrorHandler implements SubscribingHandlerInterface
{
/**
* @var TranslatorInterface|null
*/
private $translator;
/**
* @var string
*/
private $translationDomain;
2
Answers
I finally found a solution. I overridden the FormErrorHandler class from FOS/SerializerbUndle and then I overridden the formErrorHandlere class from JMS which I call in my overload of FOS/FormErrorHandler. it works perfectly
The short answer is that you can not override the final classes as long as the package author does not provide a functionality to replace the final class with a custom implmentation.
by taking a look at the source code of the package, you can do so. the
SerializeBuilder
constructor is something like:which clearly means that you can add your custom handler.
and due to the documentation you can implement your own handler easily.