i am trying to override Zend libs class like magento2 using preference

My di.xml code is

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Zend_Pdf_Resource_ImageFactory" type="\Codility\Customers\Zend\Zend_Pdf_Resource_Image"/>

But it not override . it hit to core class Zend_Pdf_Resource_ImageFactory how can we override zend lib class ?

2 Answers 2


Depends how it's been written but many of the zend classes you can simple create a new class which extends the old class and reference your new class in code

Vendor_Module_Whatever extends Zend_Pdf_Resource_ImageFactory

Then in code

$var = new \Vendor_Module_Whatever();

Within the Zend library itself the Magento DI mechanisms don't apply (ie preferences etc won't work). The only invocation that I could find looks like this:

    public static function imageWithPath($filePath)
        return Zend_Pdf_Resource_ImageFactory::factory($filePath);

You would have to move up the calling chain until you are within a Magento class to apply a different preference for it. For example Zend_Pdf_Resource_ImageFactory used by Zend_Pdf_Image used by Magento\Sales\Model\Order\Pdf\AbstractPdf (so you'll likely want the concrete class that extend of AbstractPdf)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.