Неверные значения возвращены из полей $ this -> $ xxxx - PullRequest
1 голос
/ 16 июня 2020

У меня проблема с получением значений шорткода в переменные, посоветуете пожалуйста? Это код:

class virtual_cheshire_ring

{

public $start_date              =   '';
public $end_date                =   '';
public $db_table_participants   =   '';
public $db_table_log            =   '';
public function __construct()
{
    add_shortcode('virtual_crr', array($this, 'virtual_crr') );
}

public function virtual_crr($atts)
{   
    //******************************
    //** Get shortcode parameters **
    //******************************
    global $post;
            $shortcode_defaults = [ 'start_date' => '2020-01-01',
                            'end_date'  =>  '2050-00-01',
                            'db_table_participants' =>  'db_table_participants',
                            'db_table_log'  =>  'db_table_log',
                          ];
    $attributes = array_merge($shortcode_defaults,$atts);

    $this->$start_date              = $attributes['start_date'];
    $this->$end_date                = $attributes['end_date'];
    $this->$db_table_participants   = $attributes['db_table_participants'];
    $this->$db_table_log            = $attributes['db_table_log'];

    var_dump($attributes);

    $html  = 'start_date = ' . $this->$start_date . ' / ' . $attributes['start_date'] . '<br>';
    $html .= 'end_date = ' . $this->$end_date . ' / ' . $attributes['end_date'] . '<br>';
    $html .= 'db_table_participants = ' . $this->$db_table_participants . ' / ' . $attributes['db_table_participants'] . "<br>";
    $html .= 'db_table_log = ' . $this->$db_table_log . ' / ' . $attributes['db_table_log'] . '<br>';
    return $html;
}

}

Короткий код на веб-странице: [virtual_crr start_date = «2020-06-02» end_date = "2020-06-30"]

var_dump ($ attributes) возвращает:

array (size = 4) 'start_date' => string '2020-06-02' (длина = 10)

'end_date' => строка '2020-06-30' (длина = 10)

'db_table_participants' => строка 'db_table_participants' (length = 21)

'db_table_log' => string 'db_table_log' (length = 12)

Вывод на веб-странице:

start_date = db_table_log / 2020-06-02

end_date = db_table_log / 2020-06-30

db_table_participants = db_table_log / db_table_participants

db_table_log = db_table_log / db_table_log

Очевидно, я не понимаю чего-то фундаментального, как ' Значения $ this -> $ xxxx отличаются от значений массива $ attributes, не могли бы вы посоветовать, пожалуйста?

Заранее большое спасибо

Алан

1 Ответ

0 голосов
/ 17 июня 2020

Я думаю, вам нужно удалить $ из параметров, когда вы обращаетесь к ним с помощью $this.

Так что это будет больше похоже:

<?php
public function virtual_crr($atts)
{   
  //******************************
  //** Get shortcode parameters **
  //******************************
  global $post;

  $shortcode_defaults = [
    'start_date' => '2020-01-01',
    'end_date'  =>  '2050-00-01',
    'db_table_participants' =>  'db_table_participants',
    'db_table_log'  =>  'db_table_log',
  ];
  $attributes = array_merge($shortcode_defaults,$atts);

  $this->start_date              = $attributes['start_date'];
  $this->end_date                = $attributes['end_date'];
  $this->db_table_participants   = $attributes['db_table_participants'];
  $this->db_table_log            = $attributes['db_table_log'];

  var_dump($attributes);

  $html  = 'start_date = ' . $this->start_date . ' / ' . $attributes['start_date'] . '<br>';
  $html .= 'end_date = ' . $this->end_date . ' / ' . $attributes['end_date'] . '<br>';
  $html .= 'db_table_participants = ' . $this->db_table_participants . ' / ' . $attributes['db_table_participants'] . "<br>";
  $html .= 'db_table_log = ' . $this->db_table_log . ' / ' . $attributes['db_table_log'] . '<br>';

  return $html;
}
?>
...