Solved

looking for a class or method to avoid code repeating

Posted on 2013-12-06
9
258 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

733 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