我有这个班级
class TableInvoices extends WP_List_Table {
function prepare_items() {
$sumOwed = 'Anything';
$sumTotal = 'Something';
}
}
如何在课堂外回复$ sumTotal?
由于
编辑:
class TableInvoices extends WP_List_Table {
public $sumOwed;
public $sumTotal;
function prepare_items() {
$sumOwed = 'Anything';
$sumTotal = 'Something';
$this->sumOwed = $sumOwed;
$this->sumTotal = $sumTotal;
}
}
$sum = new TableInvoices();
$sum->prepare_items();
echo $sum->sumOwed . '<br>';
echo $sum->sumTotal;
答案 0 :(得分:0)
class TableInvoices extends WP_List_Table {
public static $sumTotal;
public function prepare_items() {
sumOwed = 'Anything';
self::sumTotal = 'Something';
}
}
echo TableInvoices::$sumTotal;
使用静态变量。这将帮助您在不实例化对象的情况下访问方法和属性。
显然它会给出undefined,所以你需要实例化一个对象并运行prepare_items()
或静态访问它。
class TableInvoices extends WP_List_Table {
public static $sumTotal;
public static function prepare_items() {
self::sumTotal = 'Something';
}
}
TableInvoices::prepare_items();
echo TableInvoices::$sumTotal;