We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

multi level \Phalcon\Config and access to property

Hello,

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 =(



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


5.4k
<?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 []



3.1k
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.

e.g.:

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!



5.4k

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){
    if(!$this->isCaseSensitive()){
        $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;
}


3.1k

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



5.4k

NFR for 3.1.0

https://github.com/phalcon/cphalcon/issues/12221

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