0

I am having a problem with accessing Class methods using variable instead of function name. Like in below example:

class Test
{
    public static function route()
    {
        $functionName = "myFunction";
        echo self::$functionName;

    }

    public static function myFunction()
    {
        return "Hello";
    }
}

I am getting error message:

Fatal error: Access to undeclared static property: Test::$functionName

Is there a way of accessing class methods using variables containing function name? Please help :)

2
  • 5
    self::$functionName() Commented Jan 10, 2016 at 19:53
  • Silly me, that's exactly what I wanted. Thank you. Commented Jan 10, 2016 at 19:55

1 Answer 1

2

Calling a function usually done with (). So you have to add () to your echo self::$functionName; so it will become:

public static function route()
{
    $functionName = "myFunction";
    echo self::$functionName();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.