如何在 Magento 2 中使用 Zend_Barcode 库?

How to use Zend_Barcode library in Magento 2?

我想使用 Zend 库生成条形码,但出现以下错误:

Class 'Namespace\Module\Model\Order\Pdf\Zend_Barcode' not found in 'Namespace\Module\Model\Order\Pdf\Shipment.php'.

这是我的错误代码:

$imageResource = Zend_Barcode::draw(
            'code39', 'image', $barcodeOptions, $rendererOptions
        );

在 Magento 1.9 中有效,但当我在 Magento 2 中更改它时无效。我不知道如何在我的 Shippment class.

中包含 Zend Library Zend_Barcode class

您还可以在 class 文件的顶部导入 Zend_Barcode class:

use Zend_Barcode;

然后,您的代码将正常工作:

$imageResource = Zend_Barcode::draw(
        'code39', 'image', $barcodeOptions, $rendererOptions
    );

或者,你可以直接实例化class的对象。为此,您不需要导入 class,但必须在 Zend_Barcode class 名称前使用 "backslash"。

$imageResource = \Zend_Barcode::draw(
        'code39', 'image', $barcodeOptions, $rendererOptions
    );

参考: PHP Using namespaces: Aliasing/Importing

上的文档