Ludwig Arcache Ludwig Arcache - 3 months ago 9
PHP Question

Pass php variables from a method to outside class

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;

Answer
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;