How to detect where a function call came from..

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
tommytx123
New php-forum User
New php-forum User
Posts: 20
Joined: Sat Sep 19, 2015 4:57 pm

Sat Sep 19, 2015 5:05 pm

Is there an easy way to stop the program and indicate where the subroutine was called from..
<?php
function writeMsg() {
echo "Hello world!";
}

writeMsg(); // call the function
?>
Bottom line if I halt the program at the function.. how can I display what the call was that called the program..
I realize I could search for writeMsg() but it will be called from many different points in the program at different times..
I would like to have it log each call and where it came from...

thanks.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Fri Sep 25, 2015 5:41 pm

Probably the easiest way would be to just explicitly pass the name of the calling function as a parameter to the function:

Code: Select all

<?php

function whoSaidThat($phrase = '', $speaker = 'someone'){
    return '"' . $phrase . '," ' . $speaker . ' said.<br />';
}

function sean($tosay = ''){
    echo whoSaidThat($tosay,'Sean');
}

function cameron($tosay = ''){
    echo whoSaidThat($tosay,'Cameron');
}

sean('blah blah blah');
cameron('etcetera and what not');
echo whoSaidThat("that's what", "she");
outputs:

Code: Select all

"blah blah blah," Sean said.<br />"etcetera and what not," Cameron said.<br />"that's what," she said.<br />

tommytx123
New php-forum User
New php-forum User
Posts: 20
Joined: Sat Sep 19, 2015 4:57 pm

Mon Oct 26, 2015 5:22 am

This is a program that is already written so I do not have the ability to tell it where the call came from.. I i could do that then I would already know where it came from..
I am studying an existing program and lets say I am browsing thru the program and notice a function doWork() and am interested in seeing just what parts of the program call this particular function.. so that I might visit that section and better understand how the program works...

so it sounds like there is no easy way.. unless backtrace or something like that is used.. where I could break the program at the function.. and see if backtrace or debug would give the last line of code before arriving here.. but not being familiar with backtrace not sure if that would do it..

thanks for the help.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Tue Oct 27, 2015 7:28 am

If you're able to view the source, why can't you just search for calls to the function?

Post Reply