multi level \Phalcon\Config and access to property


I've a multi level configuration, for example:

$config = new \Phalcon\Config([
    'system' => [
        'api' => [
            'vesion': 2,
            'status': 'active'

How can i get $config['system']['api']['version'] using $config->get(...) ? It'll be awesome if I can use it the following way

$config->get('system.api.version', 1);

but it doesn't work right now.

Any suggestions? Thank you.

PS. property can absent and it can cause a fatal error or notice.

PSS. of course i've extended default class to support it, but i dislike that way =(

new \Phalcon\Config(array(
                    'system' => array(
                            'api'  =>array(
                                        'vesion'=> 2,
                                        'status'=> 'active'


>```php ><?php >new \Phalcon\Config(array( > 'system' => array( > 'api' =>array( > 'vesion'=> 2, > 'status'=> 'active' > ) > ) > ); > >```

I don't think it will help me with "get" function. It's same what i wrote above, just used array() instead of []

edited Oct '14

You can create a class that extends from Phalcon\Config and override the "public mixed get (string $index, [mixed $defaultValue])" function to implement this specific behaviour.


class DotConfig extends \Phalcon\Config {
    public function get ($index, $defaultValue) {
        $dotFound = strrpos($index, ".");
        if ($dotFound === false) {
            return parent::get($index, $defaultValue);
        } else {
            $getString = str_replace(".", "->", $index);
            return parent::get($getString, $defaultValue);

I'm very rusty in PHP, so the above code may not be exactly what you need to get it working, but the idea should be correct. Good luck!


Problem that get() function don't accept $index like 'system->api->version', according your code example.

Though, yes, you're right i can extend default class, i did it by following way:

public function get($index, $defaultValue = null){
        $index = Text::lower($index);
    //unify and split index
    $list = $this->splitIndex($index);
    //get result
    $result = $this;
    foreach($list as $option){
        if(!array_key_exists($option, $result)){
            return $defaultValue;
        $result = $result[$option];

    return $result;


Cool, glad you got it sorted out! ::thumbsup:: :)


>NFR for 3.1.0 > >

That's good new, pity that 2 years later. Anyway, thank you for information.