0

Is this possible?

Pseudo-code:

class MyClass
{
  function myFunc1()
  {
     ...
  }
  function myFunc2()
  {
    echo GET_FUNCTION_NAME($this->myFunc1)
  }
}

Wanted output:

myFunc1

In the code above the GET_FUNCTION_NAME method/function/construct/whatever would give back the textual representation of the function name given as parameter.

So the main point would be to get the name of a function as a string from outside the function.

All the code I have found deals with giving a function name via a string (eg. specifying callback methods), but none of them mentions how to get that function name without manually writing it in a string (thus duplicating code in a string and making refactoring harder than needed).
OTOH from inside the function it is easy with eg. __FUNCTION__ variable, so I'm not looking for that.

EDIT

A typical use case would be any callback method.
One example where I confronted this problem is the set_error_handler() method where it awaits a callable as first parameter. The callable can be simplified as a string. The problem is that if I specify the function name as a string, any time in the future when I will do refactoring I will have to take extra care to search for the strings as well and do special handling of them otherwise wrong references will be left there.
Not to mention the principle that any name should be defined once and any other use should refer to that one.

14
  • Check this post stackoverflow.com/questions/1826503/… Commented Aug 1, 2019 at 12:06
  • 5
    This smells like an XY problem Commented Aug 1, 2019 at 12:09
  • 1
    in case you really really need that - why don't you echo "myFunc1" ? Commented Aug 1, 2019 at 12:10
  • 3
    @B001ᛦ I think the point was to get it "without manually writing it in a string", as per the question wording Commented Aug 1, 2019 at 12:11
  • 1
    Could you make use of anonymous functions instead? Is using the string form of callable critical for some reason? Commented Aug 2, 2019 at 15:46

2 Answers 2

2

If the problem is that You need to specify a callback not using a string, but the function symbol itself, You can do with a help of anonymous function:

class MyClass {

    function call(callable $c) {
        ...
    }

    function mycallback() {
        ...
    }

    function dosomejob() {
        $this->call(function() { $this->mycallback(); })
    }
}

From point of view of Your refactoring tool, there's still call to function mycallback, it's not reffered as a string.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the post, but as I already wrote in a comment to the original question, using a forwarder (anonymous) function still leads to a quite messy code. I upvote it as it is possibly the most usable workaround, but it is still just a workaround, not a proper solution.
@Tylla Thank You, I missed Your comment to the original question (until now). I am afraid that the most readable PHP can do is PHP 7.4's fn() => $this->mycallback(). It would be nice if there was something like MyClass::class for functions, but unforunately there's not.
0

As of PHP 8.1 there's a syntax for it - https://www.php.net/manual/en/functions.first_class_callable_syntax.php

class MyClass
{
  function myFunc1()
  {
     ...
  }
  function myFunc2()
  {
    $callable = $this->myFunc1(...);
    $callable();
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.