rgb192
asked on
overwriting a method
from
https://www.experts-exchange.com/questions/28396118/is-php-errormsg-a-usable-variable-Why-is-the-try-catch-not-working.html
what is being overwritten
getsummaryline()
is the parent/child overwritten?
https://www.experts-exchange.com/questions/28396118/is-php-errormsg-a-usable-variable-Why-is-the-try-catch-not-working.html
what is being overwritten
getsummaryline()
is the parent/child overwritten?
Note that this is a gigantic improvement in the workings of PHP. In procedural PHP, a duplicate function name causes a fatal error. Only in OOP can a child class replace the function (method) of a parent.
I guess you want to know how the overwriting of the methods work here in detail, so see here:
this is the parent method (ShopProduct):
And here the child CDProduct's method
As you can see, the latter calls the parent method result and add to it an additional string, so the output of the child method will be the output of the parent method plus a new data specific to the CD, that is the playing time.
The Book product will do the same thing adding the number of pages instead of the playing time. So the parent method returns the title of the product, tthe producer full name and the descendant clases add to these infos their specific data.
Hope to have correctly interpreted your question.
Cheers
this is the parent method (ShopProduct):
function getSummaryLine() {
$base = "{$this->title} ( {$this->producerMainName}, ";
$base .= "{$this->producerFirstName} )";
return $base;
}
And here the child CDProduct's method
function getSummaryLine() {
$base = parent::getSummaryLine();
$base .= ": playing time - {$this->playLength}";
return $base;
}
As you can see, the latter calls the parent method result and add to it an additional string, so the output of the child method will be the output of the parent method plus a new data specific to the CD, that is the playing time.
The Book product will do the same thing adding the number of pages instead of the playing time. So the parent method returns the title of the product, tthe producer full name and the descendant clases add to these infos their specific data.
Hope to have correctly interpreted your question.
Cheers
ASKER
Yes, the child class overrides the parent's method. In this case it extends it.
I think overwrite is
function getSummaryLine() {
//I comment this line because this will extend, not overwrite $base = parent::getSummaryLine();
$base .= ": playing time - {$this->playLength}";
return $base;
}
Overwriting could totally replace the ancstor mthods, but usually it dosn't: it inherits the methods and add to it something specific. If one dosn't need to inherit a from a commn methods, he can ue interfaces instead. An intrface just lit the mthods one child class must hav, but it doesn't implemet none of thm, so child classes write their implementation of thos method from scratch.
http://www.php.net/manual/en/language.oop5.interfaces.php
http://www.php.net/manual/en/language.oop5.interfaces.php
See if this makes sense. The Animal can speak(). The exact contents of the vocalization is different for Cat and Dog, therefore they create different contents for the noise property. And they use different methods to produce the noise. In class and extensions, there are three versions of speak(). We use the scope-resolution operator to tell PHP which version of the method to run.
<?php // demo/temp_rgb192.php
error_reporting(E_ALL);
// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28396128.html
Class Animal
{
protected function speak()
{
echo $this->noise;
}
}
Class Cat Extends Animal
{
public function __construct()
{
$this->noise = 'Meow ';
}
public function speak()
{
parent::speak();
}
}
Class Dog Extends Animal
{
public function speak()
{
$this->noise = 'Woof ';
echo $this->noise;
parent::speak();
}
}
$pussy = new Cat;
$pussy->speak();
$puppy = new Dog;
$puppy->speak();
ASKER
Overwriting could totally replace the ancstor mthods, but usually it dosn't: it inherits the methods and add to it something specific. If one dosn't need to inherit a from a commn methods, he can ue interfaces instead. An intrface just lit the mthods one child class must hav, but it doesn't implemet none of thm, so child classes write their implementation of thos method from scratch.
so adding to $base variable is still considering overwriting
interface parent methods are declared but never used, so when required methods are used in child (implementation) class then this is also considered overwriting
Ray example:
cat uses parent method speak and same noise property from cat
dog echos noise property and then calls parent method speak and same noise property from dog which is echoed again
So both are considering overwriting?
I'm sorry -- I don't know how to explain this in any way that is better or more clear. I'll have to sign off and let another expert try to help you.
Yes, it is overwriting.
The Ray example is quite simple and clear, and i one of tha classic cases used in oop manuals to explain oop.
There you can see inheritance in action: both dog and cat are animals, so they inherit common characteristic from Animals class. In the example, they inherit tha ability to speak.
But cats and dogs are different and they 'speak' two different 'languages': so, when they born they have two different noises: in the cosntructor method (something like obstetrical department in a hospital) thay have two different noises 'meow' and 'woof'. When thy speak, they use the common method inherited from the fact they are both animals, but speaking they do two different noises.
Interfaces are slightly different: they don't implement any method but force all descendant class to implement the methods they have simply declared. o Interface 'Animals' can declare a method called 'eat' because all animals eat, but it doesn't implement because different animals eat different things, so let the descendant class 'species' speciify what each species eat.
Hope this is clear enough :)
The Ray example is quite simple and clear, and i one of tha classic cases used in oop manuals to explain oop.
There you can see inheritance in action: both dog and cat are animals, so they inherit common characteristic from Animals class. In the example, they inherit tha ability to speak.
But cats and dogs are different and they 'speak' two different 'languages': so, when they born they have two different noises: in the cosntructor method (something like obstetrical department in a hospital) thay have two different noises 'meow' and 'woof'. When thy speak, they use the common method inherited from the fact they are both animals, but speaking they do two different noises.
Interfaces are slightly different: they don't implement any method but force all descendant class to implement the methods they have simply declared. o Interface 'Animals' can declare a method called 'eat' because all animals eat, but it doesn't implement because different animals eat different things, so let the descendant class 'species' speciify what each species eat.
Hope this is clear enough :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok I now understand that animal speaking is overwriting because each animal speaks different language and not the same parent language.
Is interfaces inheritance?
Interfaces are slightly different: they don't implement any method but force all descendant class to implement the methods they have simply declared. o Interface 'Animals' can declare a method called 'eat' because all animals eat, but it doesn't implement because different animals eat different things, so let the descendant class 'species' speciify what each species eat.Is interfaces overwriting?
Is interfaces inheritance?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
overwrite empty methods
interface as forced overwriting;
thanks
interface as forced overwriting;
thanks
$product1 and $product2 use their own version of getsummaryline()