Hi everyone,
I am starting to learn PHP OOP and would like to know if you could help me understanding the scopes a little bit better.
I have created the below code during my courses, but cannot work out how to reuse $this->total in another class.
I have set the $total; to protected and was wishing to be able to use it in my second class but it returned an error.
Any idea how I could use this variable in another class while set with the scope of Protected please?
Thank you so much!
Class Calculator{ public $number1; public $number2; protected $total; function setNumber1($int){ $this->number1 = (int) $int; } function setNumber2($int){ $this->number2 = (int) $int; } function Calculate(){ $this->total = $this->number1 + $this->number2; } function getResult(){ return $this->total; } } $Calculator = new Calculator(); $Calculator->setNumber1(5); $Calculator->setNumber2(10); $Calculator->Calculate(); echo $Calculator->getResult() . "<br>"; Class secondCalculator extends Calculator { $this->total = $this->number3 + $this->number4; }
Ben