This question already has answers here:
How do I get the function name inside a function in PHP?
(4 answers)
Closed 8 years ago.
Is there a function that can return the name of the current function a program is executing?
Yes, you can get the function's name with the magic constant __FUNCTION__
class foo
{
function print_func()
{
echo __FUNCTION__;
}
function print_method()
{
echo __METHOD__;
}
}
$obj = new foo();
$obj->print_func(); // Returns: print_func
$obj->print_method(); // Returns: foo::print_method
Maybe via debug_backtrace http://www.php.net/manual/en/function.debug-backtrace.php
Related
This question already has answers here:
Sharing var from one function to the other function in PHP Class
(2 answers)
Closed 4 years ago.
Let's say I have two different functions, and one of them has a defined variable. In the second function, I don't wanna write the same variable again, can I simply use the variable from the first function in the second one WITHOUT redefining it in the second function?
Someting like:
class example{
public function a($foo){
$foo2 = $foo + 1
return $foo2;
}
public function b($foo2){
echo "result: " . $foo2;
}
}
It's simple, you can use a property $foo2 and access it from both methods:
class example{
private $foo2;
public function a($foo){
$this->foo2 = $foo + 1
return $this->foo2;
}
public function b(){
echo "result: " . $this->foo2;
}
}
$obj = new example();
$obj->a(5);
$obj->b(); // result: 6
This question already has answers here:
How to get name of calling function/method in PHP? [duplicate]
(10 answers)
Closed 7 years ago.
Is there any way (dirty is fine, this is only for debugging) to get the __FUNCTION__ (function name) from where a function was called from?
example;
function a() {
//code
return b();
}
function b() {
return __PARENT_FUNCTION__
}
echo a();
This should yield a as the function name that was originally called for this output
Take a look at debug_backtrace
function a() {
//code
return b();
}
function b() {
return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]["function"];
}
echo a();
This question already has answers here:
How do I immediately execute an anonymous function in PHP?
(9 answers)
Closed 9 years ago.
I would like to have the functionality to set a function in a class (function setFunction). Later I want to get the function (with getFunction), which I can call from outside (specifying parameters etc. ). Here is what I have tried:
class Test
{
var $function;
function setFunction($foo)
{
$this->function = $foo;
}
function getFunction()
{
return $this->function;
}
function foo1($a)
{
print "foo1: ".$a."\n";
}
function foo2($b)
{
print "foo2: ".$b."\n";
}
}
$oClass = new Test();
$oClass->setFunction($oClass->foo1);
$oClass->getFunction()('test'); # <--- line 32
The expected output is
foo1: test
But I get an error instead:
PHP Parse error: syntax error, unexpected '(' in tester.php on line 32
Any ideas how to solve this? Here are some constraints:
This has to work for arbitrary parameters for each function.
The function to be defined is always specified in the class itself (if that helps).
You have to use call_user_func:
$oClass = new Test();
$oClass->setFunction(array($oClass, 'foo1'));
call_user_func($oClass->getFunction(), 'test');
array($oClass, 'foo1') is a callable, which represents an invocation of method foo1 on class instance $oClass.
function in php is not a object what so ever. you can not do in this way.
This question already has answers here:
Closed 11 years ago.
actually I wish to create a function which combine all similar functions with similar function name. For example,
<?php
//this function carry the function name I wish to create. For example:function try
a("try");
function a($arg)
{
//I wish to create function with the name carried by $arg
//but function $arg won't work
}
?>
Anyone can guide me regarding such function?
PHP is able to use variables as call-time function names so all you would have to do is
function a($arg)
{
if (!function_exists($arg)) {
throw new InvalidArgumentException(
sprintf('"%s" is not a valid function', $arg));
}
$arg();
}
If you must go down the functional path, you should do it properly
function a(Closure $callback)
{
$callback();
}
a(function() {
echo 'This is a callback';
});
or even
$b = function() {
echo 'I am an anonymous function';
};
a($b);
See http://php.net/manual/en/functions.anonymous.php
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Caller function in PHP 5?
Like this:
function foo(){
do_something();
}
function do_something(){
// How can I find out if this function was called from "foo" ?
}
Is this possible in PHP?
(Note that in my case the do_something() function is actually a class method)
You want to use debug_backtrace() (manpage)
You can use debug_backtrace, which will let you access the call stack.
function do_something(){
$trace = debug_backtrace();
if($trace[1]['function'] == 'foo'){
// called from foo
}
}
Have a look at Caller function in PHP 5?