I've a PHP class doing stuff:
<?php
class Cart {
//....
}
$cart = new Cart();
$cart->init();
function deposit_total(){
global $cart;
$totals = array_sum( $cart::$deposits );
return $totals;
}
How is possible to use the $cart
just instanciated in a function (deposit_total()
) outside the class ?
Thank you
I've a PHP class doing stuff:
<?php
class Cart {
//....
}
$cart = new Cart();
$cart->init();
function deposit_total(){
global $cart;
$totals = array_sum( $cart::$deposits );
return $totals;
}
How is possible to use the $cart
just instanciated in a function (deposit_total()
) outside the class ?
Thank you
Share Improve this question asked Feb 26, 2022 at 12:24 Sébastien SerreSébastien Serre 4843 silver badges9 bronze badges1 Answer
Reset to default 0I've done something like that:
<?php
class Cart {
protected static $_instance = null;
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
//....
}
$cart = new Cart();
$cart->init();
function EVA_Cart() {
return Cart::instance();
}
function deposit_total(){
$cart = EVA_Cart();
$totals = array_sum( $cart->deposits );
return wc_price( $totals );
}
Is it the best way ?