tags:

views:

33

answers:

2

I have a PHP 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?

+3  A: 

You can define a "magic" method __call

valya
this looks awesome. Just a quick thought, is there a PHP 4 equivalent?
Ashley Ward
No, there is not. btw: why on Earth are you using PHP4?
Crozin
I'm not fortunately - however the system I'm developing may be deployed on a PHP4 server at some point - hopefully not....
Ashley Ward
+1  A: 

See: __call()

ircmaxell