Frankly this seems like a pretty bad design choice, however it would be possible to do this using call stack introspection using the PHP builtin debug_backtrace
function. The following example is from the php documentation for debug_backtrace
:
<?php
// filename: /tmp/a.php
function a_test($str)
{
echo "\nHi: $str";
var_dump(debug_backtrace());
}
a_test('friend');
?>
<?php
// filename: /tmp/b.php
include_once '/tmp/a.php';
?>
If b.php is executed, the output could look like this:
Hi: friend
array(2) {
[0]=>
array(4) {
["file"] => string(10) "/tmp/a.php"
["line"] => int(10)
["function"] => string(6) "a_test"
["args"]=>
array(1) {
[0] => &string(6) "friend"
}
}
[1]=>
array(4) {
["file"] => string(10) "/tmp/b.php"
["line"] => int(2)
["args"] =>
array(1) {
[0] => string(10) "/tmp/a.php"
}
["function"] => string(12) "include_once"
}
}
If you were clever you could use the function name of the function in the backtrace to call it, e.g. debug_backtrace()[1]['function']()
, but this will only work if the function is defined in the scope that you are currently executing in. See the php documentation on variable functions for more info on calling functions by their name from a string.
In my opinion, though, there should be no reason for you to do this in a well designed program. Perhaps you should think about using objects and references to objects instead.