wordpress - 从 $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 数组值不同,你能建议吗?
提前谢谢了
艾伦
解决方案
我认为您需要$
在使用 访问参数时从参数中删除$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;
}
?>
推荐阅读
- javascript - ajax 和 php 文件从输入上传未定义的索引
- jquery - jQuery令牌输入不显示数据标签rails
- snmp - 我可以将 SNMP 陷阱发送到不同的网络吗?
- c - 如何判断当前目录是否是 C 中的根目录?
- javascript - Map、findIndex 和 filter 组合
- c# - 根据列表框是否为空,使用 sql 插入行
- vba - 如何使用 VBA 在 Excel 注释中查找和替换日期格式
- python - 除 ValueError 外的 elif
- angular - 进口 angular/core 和 angular/cli 之间的区别
- java - 如何在 jar 中使用主类进行火花提交?