{"record":{"id":"beb40128f55f44f3","repo":"Intervention/image","slug":"specialized-class-static-class-must-override","errorCode":null,"errorMessage":"Specialized class '{static::class}' must override encode()","messagePattern":"Specialized class '(.+?)' must override encode\\(\\)","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Drivers/AbstractEncoder.php","lineNumber":36,"sourceCode":"{\n    use CanBuildStream;\n\n    /**\n     * Default encoding quality.\n     */\n    public const int DEFAULT_QUALITY = 75;\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see EncoderInterface::encode()\n     *\n     * @throws LogicException\n     */\n    public function encode(ImageInterface $image): EncodedImageInterface\n    {\n        if ($this instanceof SpecializedInterface) {\n            throw new LogicException(\n                \"Specialized class '\" . static::class . \"' must override encode()\",\n            );\n        }\n\n        return $image->encode($this);\n    }\n\n    /**\n     * Build new stream, run callback with it and return result as encoded image.\n     *\n     * @throws InvalidArgumentException\n     * @throws StreamException\n     */\n    protected function createEncodedImage(callable $callback, ?string $mediaType = null): EncodedImage\n    {\n        $stream = self::buildStreamOrFail();\n        $callback($stream);\n","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/AbstractEncoder.php#L18-L54","documentation":"AbstractEncoder::encode() is the generic fallback that forwards to $image->encode($this); encoder classes marked as SpecializedInterface must override it with a real implementation. Hitting this LogicException means a specialized encoder class was left without its own encode() method - an authoring defect in a custom or third-party encoder, not a usage error.","triggerScenarios":"Encoding with a custom/third-party specialized encoder that implements SpecializedInterface but does not override encode() - e.g. an encoder copied from a built-in one with the encode() body removed, reached via toJpg()/toPng()/encode().","commonSituations":"Writing custom specialized encoders (extra formats, extra drivers) and forgetting the override; partial copies of built-in encoders.","solutions":["Add the encode(ImageInterface $image): EncodedImageInterface override to the specialized encoder class","Do not implement SpecializedInterface on encoders that are meant to stay generic","If the encoder comes from a package, report it upstream and pin to a working version meanwhile"],"exampleFix":"// before\nclass AvifEncoder extends GenericAvifEncoder implements SpecializedInterface\n{\n    // encode() missing\n}\n\n// after\nclass AvifEncoder extends GenericAvifEncoder implements SpecializedInterface\n{\n    public function encode(ImageInterface $image): EncodedImageInterface\n    {\n        // driver-specific encoding...\n    }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"function encoderOverridesEncode(string $class): bool\n{\n    return (new \\ReflectionMethod($class, 'encode'))->getDeclaringClass()->getName() !== \\Intervention\\Image\\Drivers\\AbstractEncoder::class;\n}","tryCatchPattern":"try {\n    $encoded = $image->encode(new MySpecializedEncoder());\n} catch (LogicException $e) {\n    // encoder authoring bug - implement encode() in the specialized class\n}","preventionTips":["Always override encode() when implementing SpecializedInterface on encoders","Reflection-test custom encoders for the override in your test suite"],"tags":["encoder","abstract-method","custom-extension","logic-error"],"backgroundTag":"abstract-method-not-implemented","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}