Get values from protected property

血红的双手。 提交于 2019-12-11 15:22:42

问题


I am having issues with accessing the protected property. I am trying to query a database table of wordpress. Following is my code:

    <?php
    $table_name = 'wp_wp_pro_quiz_question';
    $current_quiz_id = $wpdb->get_var("SELECT max(quiz_id) as quiz_id FROM $table_name");

    $results = $wpdb->get_results( "SELECT * FROM $table_name where quiz_id = $current_quiz_id");
  if(!empty($results)){
     foreach($results as $row){?>
     // print_r($row->answer_data);
     print_r(unserialize($row->answer_data));
     $answer_data = unserialize($row->answer_data);
     $answers = array();
     foreach($answer_data as $obj) {
     $answers[] = $obj->get_answer;
     }
     print_r($answers);
     }           
    ?>

print_r(unserialize($row->answer_data)) gives following result:

Array ( 
[0] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => 26 [_html:protected] => 1 [_points:protected] => 1 [_correct:protected] => 1 [_sortString:protected] => [_sortStringHtml:protected] => 1 [_mapper:protected] => ) 
[1] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => 6 [_html:protected] => 1 [_points:protected] => 1 [_correct:protected] => [_sortString:protected] => [_sortStringHtml:protected] => 1 [_mapper:protected] => ) 
[2] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => 4 [_html:protected] => 1 [_points:protected] => 1 [_correct:protected] => [_sortString:protected] => [_sortStringHtml:protected] => 1 [_mapper:protected] => ) 
[3] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => 16 [_html:protected] => 1 [_points:protected] => 1 [_correct:protected] => [_sortString:protected] => [_sortStringHtml:protected] => 1 [_mapper:protected] => ) ) 

I used get to access the protected field. I pushed all the values to the array $answers but i am getting all values null. Result of print_r($answers): Array ( [0] => [1] => [2] => [3] => )

What am i doing wrong here?

Thank You


回答1:


Replace the two selects with one:

SELECT * FROM $table_name
    ORDER BY quiz_id DESC LIMIT 1

Then replace * by the list of columns that you want.



来源:https://stackoverflow.com/questions/57663434/get-values-from-protected-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!