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();
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:
Closed 11 years ago.
Possible Duplicate:
Calling closure assigned to object property directly
If I have a class like this:
class test{
function one(){
$this->two()->func(); //Since $test is returned, why can I not call func()?
}
function two(){
$test = (object) array();
$test->func = function(){
echo 'Does this work?';
};
return $test;
}
}
$new = new test;
$new->one(); //Expecting 'Does this work?'
So my question is, when I call function two from function one, function two returns the $test variable which has a closure function of func() attached to it. Why can I not call that as a chained method?
Edit
I just remembered that this can also be done by using $this->func->__invoke() for anyone that needs that.
Because this is currently a limitation of PHP. What you are doing is logical and should be possible. In fact, you can work around the limitation by writing:
function one(){
call_user_func($this->two()->func);
}
or
function one(){
$f = $this->two()->func;
$f();
}
Stupid, I know.
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?
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