6

Possible Duplicate:
Caller function in PHP 5?

I would like to know from where a global function or public method is being called. I guess I could do it by inspecting debug_backtrace but I'd rather use a lighterweight mechanism if one exists. Any suggestions?

For example something like so, if you imagine the get_callee() function and constant existing:

function doSomething() {
     if(get_callee() == 'PHP_GLOBAL') { throw new IllegalAccessException(); }
     ...
}
Community
  • 1
  • 1

3 Answers3

4

Edit: Sorry, saw your note about debug_backtrace() now.

Kinda ugly but hey, if you need to do this something is wrong.

The magic is in the get_callee() function and debug_backtrace(). And yes, add some error checking if you must use this.

<?php

init();

function foo()
{
 echo 'bar called from ' . get_callee() . '<br />';
 bar();
}

function bar()
{
 echo 'foo called from ' . get_callee() . '<br />';
}

function init()
{
 echo 'init.. <br />';
 foo();
}

function get_callee()
{
 $backtrace = debug_backtrace();
 return $backtrace[1]['function'];
}

Outputs:

init..

bar called from foo

foo called from bar

Community
  • 1
  • 1
alexn
  • 57,867
  • 14
  • 111
  • 145
  • Thanks, that's also what I had... but I don't want that overhead for each method call. What I'm trying to achieve is akin to the concept of friends - a way for composites to ensure that their public members are only accessed within the composite, hence there would be a check on every call. Never mind though - if anyone steps out of line I'll just beat them to death with a spade. – Chocky Chockster Oct 04 '09 at 04:32
  • @Chocky yes, that is a much preferred solution, instead of doing this mess :) – Anti Veeranna Oct 05 '09 at 15:46
2

Why dont you simply use OO and declare your method/function private?

If you start sprinkling those get_callee() all over your code, you are creating a horrible kludge.

Anti Veeranna
  • 11,485
  • 4
  • 42
  • 63
  • @Anti Veeranna because I would require a common ancestor for unrelated classes. Say I have a User with some kind of UserHelper - they shouldn't have a common superclass but I'd need one to use member access to accomplish this. – Chocky Chockster Oct 06 '09 at 10:12
  • Ok, I see. And you are not the first one to desire that feature .. sadly there is no clean way to do it in PHP :( This debug_backtrace thing is an evil hack. Everything I have found about this is the commend at the end of http://bugs.php.net/bug.php?id=37814 – Anti Veeranna Oct 06 '09 at 13:44
2

Xdebug provides some nice functions.

<?php
  Class MyClass
  {
    function __construct(){
        $this->callee();
    }
    function callee() {
        echo sprintf("callee() called @ %s: %s from %s::%s",
            xdebug_call_file(),
            xdebug_call_line(),
            xdebug_call_class(),
            xdebug_call_function()
        );
    }
  }
  $rollDebug = new MyClass();
?>

will return trace

callee() called @ /var/www/xd.php: 16 from MyClass::__construct

To install Xdebug on ubuntu the best way is

sudo aptitude install php5-xdebug

You might need to install php5-dev first

sudo aptitude install php5-dev
svassr
  • 5,538
  • 5
  • 44
  • 63