これが以前に尋ねられたのを見たことがありますが、私の特定のケースは奇妙な人のようで、解決できません。洞察をいただければ幸いです。
変数値を持つオブジェクト プロパティにアクセスしようとしています。
$foo = new Object();
$foo->first = 'bar';
$array = array(0 =>'first', 1 =>'second');
$var = 0;
return $foo->{$array[$var]};
これにより、「通知: 未定義のプロパティ: stdClass::$first」というエラーがスローされます。中括弧を削除すると、同じ結果が返されます。
何がわからないのですか?(以下の実際のコードとエラー - エラーは Drupal Watchdog ログに記録されています。)
private function load_questionnaire_queue($type, $comparator_id, $comparing_id_array)
{
$queue = array();
$type_map = array(
0 => "field_portfolio_district['und'][0]['nid']",
1 => "field_time_period['und'][0]['tid']",
);
foreach ($this->questionnaires as $q)
{
// The commented code below works as expected
// if ($q->field_portfolio_district['und'][0]['nid'] == $comparator_id &&
// in_array($q->field_time_period['und'][0]['tid'], $comparing_id_array))
// This returns an identical error, with or without braces:
if ($q->{$type_map[$type]} == $comparator_id &&
in_array($q->{$type_map[!$type]}, $comparing_id_array))
{
$queue[] = node_view($q, $view_mode = 'full');
}
}
$this->queue = $queue;
}
Notice: プロパティが未定義: stdClass::$field_portfolio_district['und'][0]['nid'] in ComparisonChart->load_questionnaire_queue()