PHP : How to ensure script does not fail if method does not exist?
I have a P开发者_开发知识库HP script which calls methods from a class I have written. However due to the nature of the system there are occasions where the method called does not exist e.g
$snippets = new Snippets();
echo $snippets->fakeMethod();
in the above example fakeMethod()
does not exist and the script fails with a fatal error and stops altogether.
I need a solution whereby either the method just fails silently or the method is checked against all methods in the class first using method_exists()
however I cannot put if statements in the script e.g.
if(method_exists(fakemethod, snippets)){
echo $snippets->fakeMethod();
}
instead the "work" needs to be done in the class somehow. Is there a solution?
You can define a "magic" method __call
See: __call()
精彩评论