Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP OOP-静态函数和变量问题还是使用单例?_Php_Oop_Static_Cart - Fatal编程技术网

PHP OOP-静态函数和变量问题还是使用单例?

PHP OOP-静态函数和变量问题还是使用单例?,php,oop,static,cart,Php,Oop,Static,Cart,我正在创建用于税务目的的类,我需要在我的应用程序的某些页面上使用它。我需要一个一般性的建议这是一个好方法还是使用Singleton,我不想使用Singleton,而是我的类的静态版本,下面是代码: class CalculationCommon extends ObjectModel { // Customer Address Array Object public static $userCountryCode; public static $cartProducts; public s

我正在创建用于税务目的的类,我需要在我的应用程序的某些页面上使用它。我需要一个一般性的建议这是一个好方法还是使用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;
}
我在另一个类中访问该类的函数时出错,请告诉我什么是最佳实践或拇指规则

提前感谢,, 纳迪姆

您真的应该多学习一点PHP语法

如果您使用的是
静态
,则不需要实例化,但您尝试的方式不起作用

您根本不需要静态或单例,只需编写一个常规类即可。
此外,您的属性永远不应该是公共的。

为什么您需要静态或单例?我看不出有任何理由。。。
$this->thisOrderProduct //having an array for current cart products.
$calculationClass = new CalculationCommon( $this->thisOrderProduct );
echo $calculationClass::calculateGlobalTaxRate( );
$this->thisOrderProduct //having an array for current cart products.
$calculationClass = new CalculationCommon( $this->thisOrderProduct );
echo $calculationClass::calculateGlobalTaxRate( );