Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

looking for a class or method to avoid code repeating

Posted on 2013-12-06
9
Medium Priority
?
264 Views
Last Modified: 2013-12-07
<?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
0
Comment
Question by:rgb192
  • 4
  • 3
  • 2
9 Comments
 
LVL 31

Expert Comment

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

Expert Comment

by:Marco Gasi
ID: 39700648
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.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39700975
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

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: 39701765
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
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 1000 total points
ID: 39701861
This does the trick:
<?php
class Beverage{
  public function stir(){
    echo '<br>'.__FUNCTION__.' function '.get_class($this).'::' .__FUNCTION__.' method of  '.get_class($this).' 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{
}
$b =new Beverage();
$m=new Martini();
$s=new VodkaTonic();
$b->stir();
echo '<br>';
$m->stir();
echo '<br>';
$s->stir();

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 39701879
It might be easier if we did away with the compound statements and put each data element on its own line with its own label.
http://www.laprbass.com/RAY_temp_marqusG.php

<?php // RAY_temp_marqusG.php
error_reporting(E_ALL);

// stir function Martini::stir method of Martini class called
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28312004.html#a39701765

class Beverage
{
    protected $liquor = NULL;
    public function stir()
    {
        $f = __FUNCTION__;
        $m = __METHOD__;
        $c = __CLASS__;
        $t = get_class($this);
        $p = get_parent_class($this);
        echo "<br>FUNCTION $f";
        echo "<br>METHOD $m";
        echo "<br>CLASS $c";
        echo "<br>get_class() $t";
        echo "<br>get_parent_class() $p";
        echo "<br>this->liquor $this->liquor";
        echo '<br>' . PHP_EOL;
    }
    public function mix()
    {
        $f = __FUNCTION__;
        $m = __METHOD__;
        $c = __CLASS__;
        $t = get_class($this);
        $p = get_parent_class($this);
        echo "<br>FUNCTION $f";
        echo "<br>METHOD $m";
        echo "<br>CLASS $c";
        echo "<br>get_class() $t";
        echo "<br>get_parent_class() $p";
        echo "<br>this->liquor $this->liquor";
        echo '<br>' . PHP_EOL;
    }
}

// EXTEND THE BASE CLASS
class Martini extends Beverage
{
    protected $liquor = "Gin";
}
class VodkaTonic extends Beverage
{
    protected $liquor = "Vodka";
}

// CREATE OBJECT INSTANCES
$m = new Martini();
$s = new VodkaTonic();

// THE MARTINI
$m->stir();
$m->mix();

// THE VODKATONIC
$s->stir();
$s->mix();

Open in new window

See http://us2.php.net/manual/en/function.get-parent-class.php#90202 for some other interesting ideas.
0
 

Author Closing Comment

by:rgb192
ID: 39703233
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.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39703262
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. :)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39703306
What MarqusG said!  Thanks for the points, ~Ray
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

972 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