-3

I'm trying to use a foreign paying system, but have difficulties in implementing this.

the Class I'm trying to modify looks like this:

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

and I execute it via

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

My question now is: how do I get variables from $_POST in this class? E.g I'm trying to mke the currency dynamic this way..

I already tried the solution posted here: How to grab data from post to a class

But I guess I fail miserably at OOPhp :/

Thanks anyway!

Community
  • 1
  • 1
Gotschi
  • 3,175
  • 2
  • 24
  • 22
  • 1
    What's the source of `Show`? What's the default constructor's parameters? – Praveen Kumar Purushothaman Aug 13 '15 at 10:22
  • Wow, three downvotes for nothing, thnks! – Gotschi Aug 13 '15 at 10:26
  • You saw the downvotes but not the comments for one of the main reason! Great! :D – Praveen Kumar Purushothaman Aug 13 '15 at 10:26
  • I guess now you understand that you need to read the comments. – Praveen Kumar Purushothaman Aug 13 '15 at 10:28
  • 1
    Put in this terms, you need to use the global array `$_POST` in your class, simply using the global variable. But if you are posting, probably there is something missing in the code... Anyway, if this is your question, use `$_POST`. For completeness, I have to tell you that if you are implementing a payment system and you don't know well what you are doing, big issue could happen: each value coming from the user input (as the values in `$_POST`) has to be validated: you cannot simply use the value. For more info: http://stackoverflow.com/questions/4223980/the-ultimate-clean-secure-function – Aerendir Aug 13 '15 at 10:31
  • @Aerendir thanks for the short but informative info! BTW: the user gets redirected to the Shops own Mask, so there's not much validation at my point... Thanks! – Gotschi Aug 13 '15 at 10:35
  • @Gotschi It was meant to be `Shop`, sorry! Don't be too much mechanical in checking what people have written. – Praveen Kumar Purushothaman Aug 13 '15 at 10:41

1 Answers1

6

There are multiple solutions. One solution could be to add a 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();

NOTE: You can access any $_POST variable in any class method. That being said you can set it from constructor if you wish so:

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();
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50