0

我正在为税收目的创建类,我需要在我的应用程序的某些页面上使用它。我需要一个一般性建议,它是一种好方法还是使用单例,我不想使用单例,而是我的类的静态版本,这里是代码:

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

}

这是我在 abc 页面中创建此类的实例。

$this->thisOrderProduct //having an array for current cart products.
$calculationClass = new CalculationCommon( $this->thisOrderProduct );
echo $calculationClass::calculateGlobalTaxRate( );

我在另一个类中访问该类的功能时遇到错误,请建议我什么是最佳实践或经验法则?

提前致谢, Nadeem

4

1 回答 1

1
$this->thisOrderProduct //having an array for current cart products.
$calculationClass = new CalculationCommon( $this->thisOrderProduct );
echo $calculationClass::calculateGlobalTaxRate( );

你真的应该多学习一点关于 PHP 语法的知识。

如果您正在使用static,则不需要实例化,但是您尝试执行的方式不起作用。

您根本不需要静态,也不需要单例,只需编写一个常规类。此外,您的财产永远不应公开。

于 2013-11-09T00:38:55.057 回答