dongqiao5573
2016-08-21 20:27
浏览 50
已采纳

将PHP变量从方法传递到外部类

I have this class

class TableInvoices extends WP_List_Table {
    function prepare_items() {
        $sumOwed = 'Anything';
        $sumTotal = 'Something';
    }
}

How can I echo $sumTotal outside the class ?

Thanks

EDIT:

@Eisa Adil solutions worked very well, also this worked:

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;
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dry9192 2016-08-21 20:57
    已采纳
    class TableInvoices extends WP_List_Table {
        public static $sumTotal;
        public function prepare_items() {
    
            sumOwed = 'Anything';
            self::sumTotal = 'Something';
        }
    }
    echo TableInvoices::$sumTotal;
    

    Use static variables. This would help you access methods and properties without instantiating an object.

    Obviously it'll give undefined so you'll need to instantiate an object and run prepare_items() or statically access that too.

    class TableInvoices extends WP_List_Table {
        public static $sumTotal;
        public static function prepare_items() {
            self::sumTotal = 'Something';
        }
    }
    TableInvoices::prepare_items();
    echo TableInvoices::$sumTotal;
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题