cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Phalcon\Config triggers warning with integer keys

Open roman-kulish opened this issue 13 years ago • 3 comments

Consider the following example:

$config = new Phalcon\Config(array(
    '404' => 'blah'
));

It triggers Warning: Phalcon\Config::__construct(): Property should be string (in ext/kernel/object.c, line 637)

For ArrayAccess it is perfectly fine to have keys as integers. In PHP it is a valid way to access them as properties using the following syntax: $config->{'404'}

roman-kulish avatar Jan 26 '13 16:01 roman-kulish

Phalcon\Config doesn't allow numeric keys because the Phalcon\Config constructor is called recursively preserving numeric arrays as they are:

$config = new Phalcon\Config(array(
        'list' => array(1, 2, 3, 4),
        'other' => array(
                'a' => 100,
                'b' => false
        )
));
Phalcon\Config Object
(
    [list] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [other] => Phalcon\Config Object
        (
            [a] => 100
            [b] => 
        )

)

ghost avatar Jan 27 '13 01:01 ghost

OK, your answer is accepted. But it is just to remind you that PHP arrays are hash maps, Phalcon\Config implements ArrayAccess and so it is expected behaviour to be a map.

// example 1

// it is stated in documentation that Phalcon\Config walks through array recursiverly and converts 
// children arrays to Phalcon\Config instances

// The following is expected:
$list = $config->list->toArray(); // convert Phalcon\Config to array outputs warning, 
                                                       // $config->list is array already

// example 2
// normal behaviour for ArrayAccess cannot be used with numeric keys

if ( !isset($config->list[$key]) ) {
    throw new \OutOfBoundsException('Unknown key [' . $key . ']');
}

roman-kulish avatar Jan 28 '13 03:01 roman-kulish

I'm going to add a note in the docs explaining this behavior, thanks

ghost avatar Feb 01 '13 20:02 ghost