J
J
JohnDoe0002022-03-02 12:07:20
PHP
JohnDoe000, 2022-03-02 12:07:20

How to call a class method using the property value without variables and call_user_func()?

How to call a class method using the property value without variables and call_user_func()?

Class property: Method call:
private string|null $method

public function match()
    {
        return match ($this->method) {
            'Update', 'Info', 'Remove' => $this->$this->method()
        };
    }


Problem:
Fatal error: Uncaught Error: Object of class Class could not be converted to string


I believe that the solution is as simple as possible, but after searching the Internet for quite some time, I could not find a solution.
I clarify that it is required to call the method using the property value, without creating extra variables inside the method to call and without using call_user_func. $this->$this->method()does not work, and could not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JohnDoe000, 2022-03-02
@JohnDoe000

Solution (by FanatPHP ): Documentation: https://www.php.net/manual/en/language.variables.v...
$this->{$this->method}();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question