首页 > 解决方案 > 从 $this->$xxxx 字段返回的值不正确

问题描述

我在将简码值转换为变量时遇到问题,请您指教?这是代码:

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) 返回:

数组(大小=4)“开始日期”=> 字符串“2020-06-02”(长度=10)

'end_date' => 字符串'2020-06-30'(长度=10)

'db_table_participants' => 字符串'db_table_participants'(长度=21)

'db_table_log' => 字符串'db_table_log'(长度=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 数组值不同,你能建议吗?

提前谢谢了

艾伦

标签: wordpressoopthis

解决方案


我认为您需要$在使用 访问参数时从参数中删除$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;
}
?>

推荐阅读