William
William

Reputation: 477

Custom error message when calling a function that doesn't exist in a class

I have a class on GitHub (link)* and I would like to output a custom error message if the specified class doesn't exist. Is this possible? For example:

user tries to call function 123456() and it doesn't exist (main::123456()) and it doesn't exist, output the error message:

Sorry, the function 123456 does not exist or was removed.

Is this possible?

*link does no longer exist

Upvotes: 1

Views: 416

Answers (1)

kero
kero

Reputation: 10638

You can do so by overriding the magic method __call(). When doing so you must provide two arguments for the method (eg., $name and $args) otherwise this won't work.

class MyClass {
    public function __call($name, $arguments) {
        throw new Exception("failed to call method ".$name);
    }
    public function __callStatic($name, $arguments) {
        throw new Exception("failed to call static method ".$name);
    }
}

Upvotes: 1

Related Questions