1

我是 PHP 中的 OOP 新手,在通过第 3 方 API 将一些数据拉入我的程序后,我正在努力动态生成对象引用。

我撤回了一个包含以下数据的记录集:

    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 )
)

我已将此数据提取到 $badges 中,现在正尝试根据以下代码动态构建对此数据集的引用,以将其包含在表中

  foreach ($badges as $badge) {
       $badgeShortname = $badge->shortname;
       $badgeObj = '$scoutChallenge-'.'>'."$badgeShortname";
          echo "<td>";  
      echo $badgeObj;
          echo "</td>";                                                                                 
      }

我尝试了各种方法,但我似乎无法按照echo "$scoutChallenge->adventure";目前的方式动态生成参考,我似乎可以让它工作的唯一方法是使用 switch 语句,例如

  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>"; 
    }
  }

这显然不是特别可扩展的。

$scoutChallenge 是我通过 API 引入的另一个数据集,它与 $badges 数据集共享引用。

有没有办法动态地回显对象引用;像“echo $scoutChallenge-> variableElement ”这样的东西,其中 variableElement 相当于 $badgeShortname 根据 switch 语句?

4

2 回答 2

1

采用

echo $scoutChallenge->$badgeShortname

您可以在PHP 中阅读更多相关信息:变量变量

于 2012-09-14T14:30:59.590 回答
0

还要检查:

获取对象变量

我认为你可以迭代一个对象

foreach($object as $attr_name => $attr_value){
     echo $attr_value;
}

它仅适用于公共属性。

于 2012-09-14T15:15:13.550 回答