Solved

method that echos data about the method that called that method

Posted on 2013-12-05
10
292 Views
Last Modified: 2013-12-07
<?php
class Beverage{
  protected function writing(){
    echo '<br>'.__FUNCTION__.' function '.__METHOD__.' method of  '.__CLASS__.' class called<br>';
  }
  public function stir(){
    $this->writing();
  }
  public function mix(){
    $this->writing();
  }
}
$b=new Beverage();
$b->stir();
$b->mix();

Open in new window



writing function Beverage::writing method of Beverage class called

writing function Beverage::writing method of Beverage class called


I want to have a method that echos data about the method that called that method
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
  • 4
  • 2
10 Comments
 
LVL 10

Expert Comment

by:ienaxxx
ID: 39697815
You can use __CLASS__ __METHOD__ and __FUNCTION___ constants.

look here for a more detailed view:
http://www.php.net/manual/en/language.constants.predefined.php
0
 
LVL 10

Accepted Solution

by:
ienaxxx earned 167 total points
ID: 39697817
The very first example is the one you are searching for:

class trick
{
      function doit()
      {
                echo __FUNCTION__;
      }
      function doitagain()
      {
                echo __METHOD__;
      }
}
$obj=new trick();
$obj->doit();
output will be ----  doit
$obj->doitagain();
output will be ----- trick::doitagain

Open in new window

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 333 total points
ID: 39698031
The magic constants, misnamed because many are not constant at all, are useful in debugging and diagnostic efforts. When you check the man page, be sure to read the links in the "See also get_class(), get_object_vars(), file_exists() and function_exists()."

An interesting note is available here:
http://www.php.net/manual/en/language.constants.predefined.php#113736

One good use for the "magic constants" is pinpoint error visualization.  For example, this will give file paths and line numbers, adding something like this to the messages.

IN /public_html/RAY_EE_mysqli_prepare_example_fail.php LINE 107

$ins = $mysqli->prepare($sql);
if (!$ins)
{
    $err
    = "QUERY PREPARE() FAIL: "
    . $sql
    . '<br>ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' IN '
    . __FILE__
    . ' LINE '
    . __LINE__
    ;
    trigger_error($err, E_USER_ERROR);
}

Open in new window

Another interesting use enables you to follow the execution path through the implementation of an interface.  The var_dump() output can show you the exact order of the function calls.
http://www.laprbass.com/RAY_oop_interface.php

<?php // RAY_oop_interface.php
error_reporting(E_ALL);
echo "<pre>";

// AREA FORMULAE: http://www.mathsisfun.com/area.html
const PI = 3.1416;

// SEE http://php.net/manual/en/language.oop5.interfaces.php
interface Shapes
{
    public function rectangle($w, $h);
    public function triangle($b, $h);
    public function ellipse($a, $b);
}

class Geometry implements Shapes
{
    protected $shapes = array();

    public function rectangle($w, $h)
    {
        $shape["n"] = __FUNCTION__;
        $shape["w"] = $w;
        $shape["h"] = $h;
        $shape["area"]  = $w * $h;
        $this->shapes[] = $shape;
    }

    public function triangle($b, $h)
    {
        $shape["n"] = __FUNCTION__;
        $shape["b"] = $b;
        $shape["h"] = $h;
        $shape["area"]  = 0.5 * $b * $h;
        $this->shapes[] = $shape;
    }

    public function ellipse($a, $b)
    {
        $shape["n"] = __FUNCTION__;
        $shape["a"] = $a;
        $shape["b"] = $b;
        $shape["area"]  = PI * $a * $b;
        $this->shapes[] = $shape;
    }
}

class Area extends Geometry
{
    public function getTotalArea()
    {
        $total_area = 0.0;
        foreach($this->shapes as $shape)
        {
            $total_area += $shape['area'];
        }
        return $total_area;
    }
}

$x = new Area;
$x->rectangle(3,5);
$x->rectangle(2,2);
$x->triangle(5,3);
$x->ellipse(1,4);

var_dump($x);
echo $x->gettotalArea();

Open in new window

0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Author Comment

by:rgb192
ID: 39698576
ienaxxx:
 
Parse error: syntax error, unexpected T_STRING in C:\Users\Acer\Documents\NuSphere PhpED\Projects\noname257.php on line 15
Maybe it needs an echo. But can both methods be called statically?

Ray:
I do not think I can run the first code but you have provided that code to me previously.  It shows the line number of error in the method that runs and not the error method.  For example it would not be useful if every line number in the error was 13,14.

Ray's second code.
Parse error: syntax error, unexpected T_CONST in C:\Users\Acer\Documents\NuSphere PhpED\Projects\noname252.php on line 6
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39698596
What version of PHP are you running?  You should be on PHP 5.4 at least.  Anything less is back-level and needs to be upgraded for modern examples to work correctly.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39698611
This script worked perfectly for me.  You have to read the code.  When you do that you will see that there are comments interspersed in the code example, but that they needed only to have comment markers set on them!
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28310969.html#a39698576

class trick
{
      function doit()
      {
                echo __FUNCTION__;
      }
      function doitagain()
      {
                echo __METHOD__;
      }
}
$obj=new trick();
$obj->doit();
// output will be ----  doit
$obj->doitagain();
// output will be ----- trick::doitagain

Open in new window

0
 

Author Comment

by:rgb192
ID: 39700162
doittrick::doitagain

My ide is now using
PHP Version 5.3.10
0
 

Author Comment

by:rgb192
ID: 39700251
I am still working on updating all my servers and ide to php5.4

Ray's code works on
PHP Version :
5.3.13  

but not in my ide because of the word
const

what is a replacement
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 333 total points
ID: 39701386
You probably want to read up on these things.
http://php.net/manual/en/language.oop5.constants.php
http://php.net/manual/en/function.constant.php
http://php.net/manual/en/function.define.php
http://php.net/language.constants.syntax

You might try changing line 6 from this...

const PI = 3.1416;

... to something like this...

define('PI', 3.1416);
0
 

Author Closing Comment

by:rgb192
ID: 39703206
echoing method and definition of constant vs define

thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
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 …

729 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