duankanyi6539 2012-09-14 14:26
浏览 34
已采纳

在php中动态创建对象引用

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?

  • 写回答

2条回答 默认 最新

  • douwen9345 2012-09-14 14:30
    关注

    use

    echo $scoutChallenge->$badgeShortname
    

    You can read more about it in PHP: Variable variables

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂