I have the following piece of code:
$item_list = array();
$item_list['PENCIL'] = "Utility used to write.";
$item_list['CAR'] = "A means of transportation.";
function item_exists($name) {
global $item_list;
return isset($item_list[$name]);
}
function get_item_description($name) {
global $item_list;
return ( item_exists($name) ? $item_list[$name] : "Unknown item." );
}
On top of the file an array is defined which contains a list of items with descriptions, which are used by multiple functions. The array is never modified in the functions, it is only used as read-only data. If I want to rewrite this piece of code to avoid using global variables, what is the nicest way to do this?
You can encapsulate your data in a class. This is especially appropriate if you are planning to do other operations with the
$item_list.Something along these lines should get you started:
Try it out