简体   繁体   中英

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

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM