Back to Blog
Core Components

Using Dotkernel with Composer Dependencies

The Composer Dependencies

The ZendFramework 1 Barcode module can only be loaded with ZF1 itself:

composer require 'zendframework/zendframework1'

The ZendFramework 2 Barcode module can be loaded separately:

composer require 'zendframework/zend-barcode'

Important note

These dependencies can be used anywhere after the Dot_Kernel::initialize() function was called.

Using Non-Namespaced Dependencies (Zend Framework 1)

The class is loaded PSR-0 style, meaning the class name looks like VendorName_PackageName_ClassName:

// Only the text to draw is required
$barcodeOptions = array('text' => 'ZEND-FRAMEWORK');

// No required options
$rendererOptions = array();

// Draw the barcode in a new image,
// send the headers and the image
Zend_Barcode::factory(
    'code39', 'image', $barcodeOptions, $rendererOptions
)->render();

Using Namespaced Dependencies (Zend Framework 2)

The class is loaded PSR-4 style, meaning the class name looks like \VendorName\PackageName\ClassName:

use Zend\Barcode\Barcode;

// Only the text to draw is required
$barcodeOptions = array('text' => 'ZEND-FRAMEWORK');

// No required options
$rendererOptions = array();

// Draw the barcode in a new image,
// send the headers and the image
Barcode::factory(
    'code39', 'image', $barcodeOptions, $rendererOptions
)->render();

The result

Both examples render the same barcode.

Resulting barcode.

Tip

The first (ZF1-style) example will work for both namespaced and non-namespaced dependencies if you add this as the first line:

use Zend\Barcode\Barcode as Zend_Barcode;

You can then use any of the following to access ZF2's Barcode module:

  • Zend\Barcode\Barcode
  • Barcode
  • Zend_Barcode

Compatibility

This article works for any Dotkernel 1.x version if your server is running PHP greater than 5.4.0.

Frequently Asked Questions

Do I need to manually include or require Composer dependencies in Dotkernel? +

No. Composer automatically loads dependencies, so there is no need to include or require them yourself.

What example does the article use to demonstrate Composer dependencies? +

The article renders a Barcode using Zend Framework 1 as the Non-Namespaced dependency (installed with `composer require 'zendframework/zendframework1'`) and Zend Framework 2 as the Namespaced dependency (installed with `composer require 'zendframework/zend-barcode'`).

When can these Composer dependencies be used in the application? +

These dependencies can be used anywhere after the Dot_Kernel::initialize() function has been called.

How are non-namespaced (Zend Framework 1) classes loaded compared to namespaced (Zend Framework 2) classes? +

Non-namespaced ZF1 classes are loaded PSR-0 style, meaning the class name looks like VendorName_PackageName_ClassName (e.g. Zend_Barcode). Namespaced ZF2 classes are loaded PSR-4 style, meaning the class name looks like \VendorName\PackageName\ClassName (e.g. Zend\Barcode\Barcode).

Can the same code work for both namespaced and non-namespaced barcode dependencies? +

Yes. If you add "use Zend\Barcode\Barcode as Zend_Barcode;" as the first line, the ZF1-style example will work for both, and you can then reference the module as Zend\Barcode\Barcode, Barcode, or Zend_Barcode.

What Dotkernel and PHP versions does this article apply to? +

This article works for any Dotkernel 1.x version if your server is running PHP greater than 5.4.0.