Solved

looking for a class or method to avoid code repeating

Posted on 2013-12-06
9
261 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
[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
  • 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 110

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 250 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 110

Accepted Solution

by:
Ray Paseur earned 250 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 110

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

617 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