Solved

overwriting a method

Posted on 2014-03-24
13
164 Views
Last Modified: 2014-04-01
from
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28396118.html

what is being overwritten

getsummaryline()

is the parent/child overwritten?
0
Comment
Question by:rgb192
  • 4
  • 4
  • 3
  • +1
13 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39951301
Yes, the child class overrides the parent's method. In this case it extends it.

$product1 and $product2 use their own version of getsummaryline()
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39951626
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.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39951672
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):
    function getSummaryLine() {
        $base  = "{$this->title} ( {$this->producerMainName}, ";
        $base .= "{$this->producerFirstName} )";
        return $base;
    }

Open in new window


And here the child CDProduct's method
    function getSummaryLine() {
        $base = parent::getSummaryLine();
        $base .= ": playing time - {$this->playLength}";
        return $base;
    }

Open in new window


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
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:rgb192
ID: 39951867
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;
    }
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39952434
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
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39953256
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();

Open in new window

0
 

Author Comment

by:rgb192
ID: 39965456
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?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39965553
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.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39965772
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 :)
0
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 250 total points
ID: 39966795
Think of an interface as forced overwriting; you have to write your own method, as the parent class does not provide one.
0
 

Author Comment

by:rgb192
ID: 39966786
Ok I now understand that animal speaking is overwriting because each animal speaks different language and not the same parent language.


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?
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 250 total points
ID: 39966937
rgb, in oop (and in many other aspect of the life) not all is perfectly defined and distrinct.
For instance, the overwriting exists because inheritance exists. In other words, a  method can be overwritten because it is inherited: if dog didn't inherit the speak method from its parent it couldn't overwrite it.

Overwriting presupposes the inheritance and this is true for interfaces also, but in this case you overwrite ampty methods so it seems wrong to say you're overwriting, but it's so.
0
 

Author Closing Comment

by:rgb192
ID: 39970860
overwrite empty methods
interface as forced overwriting;

thanks
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question