Solved

looking for a class or method to avoid code repeating

Posted on 2013-12-06
9
255 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 30

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 30

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 108

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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

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 108

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 30

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 108

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

747 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

18 Experts available now in Live!

Get 1:1 Help Now