Would it make more sense in the following situation to create a new object type instead of using an array:
I have a simple structure with two values: Name & Age
$ages = array(
array(
'name' => 'bill',
'age' => 22
),
array(
'name' => 'bob',
'age' => 50
),
// etc
);
The problem is that this structure is generated in one class and passed through a controller and then used in another class.
Therefore it feels like those two classes are tied together as one must know the array keys of this structure that is generated in another.
Is there any design pattern that solves this?
Since it’s a simple structure you can work with it, but in general it’s recommended to work with objects. If you’ll want to add fields in the future, add levels (nested arrays) – maintenance will be easier as you’re program will be more modular and less coupled:
Further, if you’ll have calls like
IIon different places in the code, changing$agesstructure will result in changing all these places, while if you implementI– you have only one place in the code to change (the implementation ofget_age($name)inside the class).