?
Solved

method that echos data about the method that called that method

Posted on 2013-12-05
10
Medium Priority
?
297 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 668 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1332 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 111

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 111

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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1332 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 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