Reputation: 21
I'm new to OOP in PHP and am struggling to dynamically generate an object reference, after having pulled some data into my program via a 3rd party API.
I have pulled back a record set that contains the following data:
stdClass Object (
[adventure] => stdClass Object (
[shortname] => adventure
[name] => Adventure
[description] => Create a new column for each activity undertaken. The scouts need to complete three activities, and for each one:
Know the safety issues involved and understand the use of any equipment needed for the activity.
Show an awareness of environmental issues around the activity (such as erosion at popular climbing areas).
Know about further opportunities to take part in the chosen activities.
[picture] => graphics/badges/sc-cs-adch.png
[config] => {"sectionsneeded":"1","totalneeded":"1","sections":{"a":"3"}}
[order] => 1
[groupname] =>
[status] => 3
[userid] => 0
[table] => scouts_challenge_adventure )
[csg] => stdClass Object (
[shortname] => csg
[name] => Chief Scouts Gold
[description] =>
[picture] => graphics/badges/sc-cs-csa.png
[config] => {"sectionsneeded":"-1","totalneeded":"1","sections":{"a":"6","b":"2"}}
[order] => 0
[groupname] =>
[status] => 3
[userid] => 0
[table] => scouts_challenge_csg )
[community] => stdClass Object (
[shortname] => community
[name] => Community [description] =>
Create columns for each community project undertaken, and enter the number of hours each scout spend on them. 6 hours are required.
[picture] => graphics/badges/sc-cs-coch.png
[config] => {"sectionsneeded": -1, "totalneeded": -1, "sections": {"a":1}}
[order] => 1
[groupname] =>
[status] => 3
[userid] => 0
[table] => scouts_challenge_community )
)
I have pulled this data into $badges and am now trying to build references to this dataset dynamically as per the code below to include it in a table
foreach ($badges as $badge) {
$badgeShortname = $badge->shortname;
$badgeObj = '$scoutChallenge-'.'>'."$badgeShortname";
echo "<td>";
echo $badgeObj;
echo "</td>";
}
I've tried various approaches but I can't seem to dynically generate a reference along the lines of echo "$scoutChallenge->adventure";
At the moment the only way I can seem to get this to work is using a switch statement e.g.
foreach ($badges as $badge) {
$badgeShortname = $badge->shortname;
$badgeObj = '$scoutChallenge-'.'>'."$badgeShortname";
echo "<td>";
switch ($badgeShortname )
{
case 'csg':
echo "$scoutChallenge->csg";
break;
case 'adventure':
echo "$scoutChallenge->adventure";
break;
case 'community':
echo "$scoutChallenge->community";
break;
case 'creative':
echo "$scoutChallenge->creative";
break;
case 'expedition':
echo "$scoutChallenge->expedition";
break;
case 'fitness':
echo "$scoutChallenge->fitness";
break;
case 'outdoor':
echo "$scoutChallenge->outdoor";
break;
case 'outdoorplus':
echo "$scoutChallenge->outdoorplus";
break;
case 'promise':
echo "$scoutChallenge->promise";
break;
case 'global':
echo "$scoutChallenge->global";
break;
default:
echo "Not Found";
break;
}
echo "</td>";
}
}
which obviously isn't particularly scalable.
$scoutChallenge is another dataset that I have pulled in via the API and one that shares a reference with the $badges dataset.
Is there a way of echoing the Object reference dynamically; something like "echo $scoutChallenge->variableElement" where the variableElement is equivalent to the $badgeShortname as per the switch statement?
Upvotes: 1
Views: 275
Reputation: 682
Check also:
And I think you can iterate over an object with
foreach($object as $attr_name => $attr_value){
echo $attr_value;
}
It only works with public attributes.
Upvotes: 0
Reputation: 2339
use
echo $scoutChallenge->$badgeShortname
You can read more about it in PHP: Variable variables
Upvotes: 1