从$ this-> $ xxxx字段返回的错误值

问题描述 投票:1回答:1

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

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) 'start_date'=>字符串'2020-06-02'(length = 10)

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

'db_table_participants'=>字符串'db_table_participants'(length = 21)

'db_table_log'=>字符串'db_table_log'(length = 12)

网页上的输出是:

开始日期= db_table_log / 2020-06-02

结束日期= 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数组值不同,请您指教吗?

非常感谢

艾伦

wordpress oop this
1个回答
0
投票

我认为当您使用$访问它们时,需要从其参数中删除$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;
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.