Solved

method that echos data about the method that called that method

Posted on 2013-12-05
10
278 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
  • 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 108

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
 

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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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 …
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…
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.

920 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

14 Experts available now in Live!

Get 1:1 Help Now