0

I have these two functions:

    public function getSales() {
        return $this->sales;
    }
    public function getCommissionRate() {
        return $this->commission_rate;
    }

I want to multiply the sales and commission in a new method "getEarnings()" so that I can put it into a variable called $earnings

I am not sure how to make the happen. My best idea is:

    public function getEarnings() {
        return $this->sales * $this->commission_rate = $earnings;
    }

Can someone please educate me on how to do this correctly?

BondyeLwa
  • 27
  • 6

2 Answers2

0

You could use private variables with setters and getters implementation but for the time being here is an example:

class example {
    public $this->sales;
    public $this->commission_rate;
    public $this->earnings;

    public function getSales() {
        return $this->sales;
    }
    public function getCommissionRate() {
        return $this->commission_rate;
    }
    public function getEarnings() {
         $this->earnings = $this->sales * $this->commission_rate;
         return $this->earnings;
    }
}
TopCheese
  • 220
  • 1
  • 8
0

Good coding style says that if you have getters/setters in a class, use them, instead of referencing the variables directly. Check this link on public variables, getters and setters. The best way is to make your variables private and use their getters and setters, ALSO, if you have them, use them from inside the Class too.

So, you could use:

// ...
private $earnings;

public function setEarnings() {
    $this->earnings = ($this->getSales() * $this->getCommisionRate());
}

public function getEarnings() {
    return $this->earnings;
}

For information purposes, check this Java post on the same topic.

Community
  • 1
  • 1
Condorcho
  • 503
  • 4
  • 11