I'm creating Class for Tax purpose, which I need to use it on some pages in my app. I need a general suggestion is it a good way or to use Singleton, I don't want to use Singleton but a Static version of my class, here's the code:
class CalculationCommon extends ObjectModel
{
// Customer Address Array Object
public static $userCountryCode;
public static $cartProducts;
public static $shopID;
public static $theTaxRateIs;
public static $context;
public function __construct( $cartProductList )
{
self::$shopID = Context::getContext()->shop->id
self::$userCountryCode = cart::getCustomerAddressDetail($cartProductList[0]['id_address_delivery']);
self::$cartProducts = $cartProductList;
}
/*
* @param array Product obj (all products in current cart)
* Calculate the Tax Rate Globally for the cart, instead of caluculating individually everywehre.
* If Address is in Canada then check if the tax rate is Flat or Destination base
* If Outside of Canada then check the export rate whether flat or Individual attribute base
*/
public static function calculateGlobalTaxRate( )
{
//Check if any attribute is taxable then apply Tax in Cart
if( self::taxableCart())
{
if(self::$userCountryCode[0]['iso_code'] =='CA') // Inside of Canada
{
echo "CANADA<br>";
}
else
{
// Reserved for Export Rate Outside of Canada
}
}
else
$globalTaxRateIs = 0; // if No attribute prone for tax then no Tax
// self::$theTaxRateIs = $globalTaxRateIs;
return $globalTaxRateIs;
}
/*
* Check if any attribute is taxable before apply Tax in Cart
*/
public static function taxableCart()
{
return true;
}
}
Here's what I'm creating an instance of this class in an abc page.
$this->thisOrderProduct //having an array for current cart products.
$calculationClass = new CalculationCommon( $this->thisOrderProduct );
echo $calculationClass::calculateGlobalTaxRate( );
I'm getting error while accessing the function of this class in another class, Please suggest me what is the best practice or Rules of Thumbs?
Thanks in Advance, Nadeem
$this->thisOrderProduct //having an array for current cart products.
$calculationClass = new CalculationCommon( $this->thisOrderProduct );
echo $calculationClass::calculateGlobalTaxRate( );
You should really study a little bit more about PHP syntax.
If you are using static
, there is no need for instantiation, but the way you are trying to do doesn't work.
You don't need static neither singleton at all, just write a regular class. Also, your properties should never be public.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.