繁体   English   中英

PHP OOP-静态函数和变量问题或使​​用Singleton?

[英]PHP OOP - issue with Static functions and variable or use Singleton?

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

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

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

您应该真正学习更多有关PHP语法的知识。

如果您使用static ,则无需实例化,但是您尝试执行的方式无效。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM