Solved

method that echos data about the method that called that method

Posted on 2013-12-05
10
279 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 109

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 109

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 109

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 109

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 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