Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

looking for a class or method to avoid code repeating

<?php
class Beverage{
  public function stir(){
    echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
  }
  public function mix(){
    echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
  }
}
class Martini extends Beverage{
  public function stir(){
   echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
  }
  public function mix(){
    echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
  }
}
class VodkaTonic extends Beverage{
  public function stir(){
    echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
  }
  public function mix(){
    echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
  }
}
$m=new Martini();
$s=new VodkaTonic();
$m->stir();
echo '<br>';
$s->stir();

Open in new window


Is there a class or method that can be called so this
echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
is not repeated many times
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

In the derived classes you don't have to redeclare methods: they are simply inherited by the ancestor class.

<?php
class Beverage{
  public function stir(){
    echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
  }
  public function mix(){
    echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
  }
}
class Martini extends Beverage{
}
class VodkaTonic extends Beverage{
}
$m=new Martini();
$s=new VodkaTonic();
$m->stir();
echo '<br>';
$s->stir();
                                  

Open in new window


This is one of woderful things of OOP style :)
You can lear about inheritance here: http://www.php.net/manual/en/language.oop5.inheritance.php

To be brief, all derived classes inherit method and properties of the class they extend.
You can avoid code repeating for some things by using inheritance, but it really depends on what you're trying to achieve with the methods.  If there is a human class it could define the kidney method and extensions of the human would not need to override the kidney.  But they would need to override the sex method to distinguish between man and woman.  And further extensions would need to override the employment method to account for different jobs.

In this code sample, please see line 5.  You can install it and run it to see the effect.
http://www.laprbass.com/RAY_temp_marqusG.php

<?php // RAY_temp_marqusG.php
class Beverage{
  public function stir(){
    echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
    echo get_class($this);
  }
  public function mix(){
    echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
  }
}
class Martini extends Beverage{
}
class VodkaTonic extends Beverage{
}
$m=new Martini();
$s=new VodkaTonic();
$m->stir();
echo '<br>';
$s->stir();

Open in new window

Avatar of rgb192

ASKER

Rays example is the closest to being correct

stir function Beverage::stir method of Beverage class called
Martini

stir function Beverage::stir method of Beverage class called
VodkaTonic


Looking for

stir function Martini::stir method of Martini class called


stir function VodkaTonic::stir method of VodkaTonic class called
SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

Marqus: I now see that I do not need a get_method because the method name is the same. So only get_class is needed.

Ray's code has get_parent_class() so I like better.

Thanks both.
I'm glad to help you. I think your perseverance and diligence is really admirable: you're like a tank with this tutorial! Good training. :)
What MarqusG said!  Thanks for the points, ~Ray