Solved

method that echos data about the method that called that method

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

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

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
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.

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

10 Experts available now in Live!

Get 1:1 Help Now