I have a database table which stores a “type” for a project which stores either a 1, 2 or 3 where:
1 = “Active”
2 = “Inactive”
3 = “Cancelled”
Currently, I store this mapping in an array in my config.php making it a global variable accessible from my whole application. It looks something like:
$project_types = array(1 => "Active", 2 => "Inactive", 3 => "Cancelled");
Now, I have a Project class, which has get_type() and set_type() methods to alter the integer value as expected.
I want to have a get_type_name() method. Can anyone here explain what this method should look like? Currently, I have something that looks like this:
public function get_type_name() {
global $project_types;
return $project_types[$this->get_type()];
}
I the array above should somehow lay inside my Project class, but I’m just not sure what route to take.
Thanks.
Globals are bad, and in your case, creates an unnecessary dependency for your Project class.
The solution (one of many) is quite simple:
Create a class property that holds the types and do the lookup on it.