There is no need for object orientation. But switchit can be replaced by a much simpler design. If you are using a data array, you can even skip the function:
$apple_data = array(
'Fuji' => array(
'Color' => 'Yellowish green';
'Size' => 'medium';
'Origin' => 'Japan';
'Season' => 'October - January';
'AppleId' = 1234567890,
),
'Gala' => array(
'Color' => 'yellow';
'Size' => 'medium';
'Origin' => 'New Zealand';
'Season' => 'October - January';
'AppleId' => 1234598760,
),
...
);
To access the attributes you just used:
$id = $apple_data["Granny_Smith"]["AppleId"]
Or, if you really want all these local variables:
extract($apple_data["Granny_Smith"]);
// creates $Color, $Size, $Origin, $Season, $AppleId in local scope
If you really need the syntax of an object, try:
$AppleProps = new ArrayObject($apple_data["Fuji"], 2);
print $AppleProps->Color;
But since apples do nothing, you probably don't want to create a class or real objects for them. (Damn apples, just sitting and doing nothing.)
mario source
share