php most elegant function call returning array I am asking your preference between two similar solutions.

First way :

public function test($myvar) {

    switch ($myvar) {   
        case 'test1': return [
                          'value' =&gt; 'Value of test1',
                          'short' =&gt; 'Val 1'
                      ];
        break;
     }
}

$this-&gt;test('test1')['short']  // Val 1


Second way:

public function test($myvar, $key) {

    switch ($myvar) {   
        case 'test1': $array = [
                          'value' =&gt; 'Value of test1',
                          'short' =&gt; 'Val 1'
                      ];
    }

    return $array[$key];
}

$this-&gt;test('test1', 'short')  // Val 1


Even if both function return same value, what is the most elegant and readable way for you guys ?
This question is not simply preference, the second method is better than the first for the following reasons:

In the first, you initialise a function test and it returns an array: test[]
So, alongside the string to test against 'short' in the memory, you now have those three things in the memory.

Whereas, in the second example, you don't return an array, and the 'short' string is only used as a parameter, by doing this you now can only have two things in the memory.

Therefore, the second code block is better than the first.

Also, in the spirit of Code Review, you can further improve the second code block, by not actually assigning $array, or using a switch.

public function test($myVar, $key) {
    return [
        'test1' =&gt; [
            'value' =&gt; 'Value of test1',
            'short' =&gt; 'Val 1'
        ]
    ][$myVar][$key];
}

$this-&gt;test('test1', 'short')  // Val 1



$myvar should also be in camelCase: $myVar