[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

overwriting a method

Posted on 2014-03-24
13
Medium Priority
?
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
13 Comments
 
LVL 35

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 111

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 111

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 111

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 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 1000 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 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

650 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