ZF2 – Configuration Merging

One big change in ZF2 is the complete change for inheritance with configuration files; Meaning it’s no longer supported.  This can be a difficult change to make from ZF1 especially if you’ve built any or all configurations in a way that does this type of inheritance.

There are solutions to helping you obtain environmental configuration setups but that will only get you a global config, with a simple localized override.  While I don’t completely agree with non-inherited configurations, I can see the benefits.  Loading a configuration and merging it is expensive, but nothing that can’t be cached.

ZF2 didn’t completely remove this logic, they’ve hidden it, here’s a way to merge an 2 existing configurations together:

    "_base" : {
        "label" : "base label",
        "properties" : {
            "i" : {
    "attribute" : {
        "_extends" : "_base",
        "label" : "attribute label override."


use ZendConfigFactory;
use ZendStdlibArrayUtils;

public function loadConfig($type)
    $loadedConfig = Factory::fromFile('config/objects.json');
    $config = isset($loadedConfig->{$type}) ? $loadedConfig->{$type} : null;
    if (isset($config['_extends'])) {
        $configBase = $loadedConfig[$config['_extends']];
        $config = ArrayUtils::merge($configBase, $config);

    return $config;

Using the ZF1 JSON variable “_extends” in your config you can see that there’s a one to one load of the configuration file and it’s extension.  This doesn’t recursively call the function but it could easily be updated to do that.  Once the config is generated, it should be cached and returned.  This operation can get expensive and may not hold up well to load.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s