Where to store the huge constants that my OOP class needs

I am new to OOP and am wondering what is the right way to OOP. My class has a method getValueto which the input (key) is passed, which it uses to search for an array and returns a value.

The thing is that the HUGE array is about 200 records and really violates the readability of the method, so I tend to remove it from this class, but I'm not sure where to put it. Should I create a separate class for it or put it in a constant class Constants::getValue()or any other sentences? It would be wrong to remove it from this class, since it should be contained inside the object that it needs?

public function getValue($input) {
    switch ($input) {
        case 'in1' : { $value = array('wsj', 'kwo'); break; }
        case 'in2' : { $value = array('wpo', 'ki2'); break; }
        .....
        default:      { $value = array('wap', 'k90'); break; }
    }
    return $value;
}
+3
source share
2 answers

key = > php

content_array.php:

$ARRAY = array(
               'in1' => array('wsj','kwo'),
               'in2' => array('wpo','ki2'),
                ...
               'default' => array('wap','k90')
              );

:

public function getValue($input){
    include_once('content_array.php');
    if(array_key_exists($input, $ARRAY)){
        $value = $ARRAY[$input];
    } else {
        $value = $ARRAY['default'];
    }
    return $value;
}

, JSON, json_decode()

+2

, , , map/lookup, , . , , .

+1

All Articles