benoit1980 Posted April 26, 2014 Share Posted April 26, 2014 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted April 26, 2014 Share Posted April 26, 2014 Class secondCalculator extends Calculator { $this->total = $this->number3 + $this->number4; }Where are the methods? You can't just put code into a class. It has to be inside a method. secondCalculator adds the concept of a number3 and number4 so you'll need methods for that. Like "setNumber3" and "setNumber4". You'll also need a method to do the addition: either "Calculate" which will replace the one in Calculator, or another method entirely. Quote Link to comment Share on other sites More sharing options...
bambinou1980 Posted April 27, 2014 Share Posted April 27, 2014 (edited) HiThank you so much for the reply. So lets say that instead of setNumber3 and 4 i want to reuse setNumber1 and 2 from the Calculator Class? I am just trying to understand the "extends" concept as i cannot find a good example.Would it be possible to demo the "extends" on my current code please?I am trying to learn the Yii framework and knows that extends means that the function of a class extends to another one but cannot work out how to reuse them in that other class:-))At the same time why would someone extends functions from a base class rather than pulling that base class functiond directly on the file?ThanksBen Edited April 27, 2014 by bambinou1980 Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted April 27, 2014 Share Posted April 27, 2014 (edited) Hi Ben Firstly you would be best to have a setter method on your first calculator class e.g.: public function setNumbers($propName, $propValue){ $this->{$propName} = (int)$this->$propValue; } You then call that method like this to set a new property without being constrained to particular methods: $WhateverYourObjectIsCalled->setNumbers('number1', 50); Extends vs include The point of extending a class is that you inherit all of the properties from the parent class and these can be overridden with new versions of the property or methods from the extending/child class. When you extend a class you do not need to implicitly instantiate the parent class and you can access all of it's methods using $this-> - i.e. as if those properties and methods were part of the extending class. However, if you simply included the new class and instantiated you would have to access the base class' properties via an instance of that object. This is best illustrated in code: //############ Preferred inheritance route ########## class a { public $somePropFromA = 'Prop from A!'; } class b extends a{ public function getProp(){ echo $this->somePropFromA; //Can access any property of method from 'a' using $this-> } } $b = new b; $b->getProp(); //############ include route - not preferred ############## //lets imagine you have included class 'c' file here class c { public $somePropFromC = 'Prop from C!'; } //note we are not extending C in this instance class d { public function __construct(){ //because the class is now included and not extended the properties aren't inherited and can only be accessed //from an object instance - in this case c = new c; $this->c = new c; } public function getProp(){ //echo $this->somePropFromC; CANNOT NOW ACESS C's properties using $this-> because we arent inheriting them echo $this->c->somePropFromC; //this is how we must now access properties } } $d = new d; $d->getProp(); So hopefully from the code above you can see by not extending the class you lose the ability to inherit it's properties and methods - though they can still be accessed via a much more verbose syntax. It's hard to see the significance in the simplistic calculator class but once the complexity of the base class increases you then begin the see the power and flexibility afforded by inheritance via extending parent classes. I am sure that someone else will propose additional benefits but this is one of the main ones that springs to my mind. Edited April 27, 2014 by Drongo_III Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.