在 class 中获取数组信息

get array info in class

我正在尝试使用外国支付系统,但在实施时遇到困难。

我正在尝试修改的 Class 看起来像这样:

class MyShop extends Shop {
    var $currency = "EUR";
}

我通过

执行它
$myShop = new MyShop('user', 'pass', TRUE, TRUE);
$result = $myShop->pay();

我现在的问题是:如何从这个class中的$_POST 获取变量? 例如,我正在尝试以这种方式使货币动态变化..

我已经尝试过此处发布的解决方案:How to grab data from post to a class

但我想我在 OOPhp 上惨败了:/

谢谢!

有多种解决方案。一种解决方案是添加 setter:

class MyShop extends Shop
{
    var $currency = "EUR";

    public function setCurrency($currency)
    {
        $this->currency = $currency;
    }
}

$myShop = new MyShop('user', 'pass', TRUE, TRUE);
$myShop->setCurrency($_POST['currency']);

$result = $myShop->pay();

注意:您可以在任何 class 方法中访问任何 $_POST 变量。也就是说,如果您愿意,可以从构造函数中设置它:

class MyShop extends Shop
{
    var $currency = "EUR";

    public function __construct($user, $pass, $param1, $param2)
    {
        parent::__construct($user, $pass, $param1, $param2);

        if (isset($_POST['currency'])) {
             $this->currency = $_POST['currency'];
        }
    }
}

$myShop = new MyShop('user', 'pass', TRUE, TRUE);

$result = $myShop->pay();