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 ?


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();
| improve this answer | |

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)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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