开发者

PHP : How to ensure script does not fail if method does not exist?

开发者 https://www.devze.com 2023-01-03 20:42 出处:网络
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

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()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号