Solved

looking for a class or method to avoid code repeating

Posted on 2013-12-06
9
257 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 109

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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 109

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 109

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
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 …

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now