PDA

View Full Version : How to handle call to undefined method



MaKa
March 1st, 2004, 08:18 AM
Hi there,

I see that is possible with NetServices call a function that has not been defined, like this.


con = NetServices.createGatewayConnection(url);
service = con.getService(serviceName, responder);

service.getAnyMethod(10,20); // here I call getAnyMethod that has never been defined.


My question is:

How can I handle a call to getAnyMethod if it doesn't exist?

in PHP it's called overload. Converting it to flash, would be like this:


class AnyClass{
function __call(m, a){
// m is the method name
// a are the parameters
}
}

senocular
March 1st, 2004, 09:15 AM
There is a call in Flash, but it calls existing functions allowing you to call functions in the scope of a specified object though it may exist in the scope of another. But what I think you're looking for is __resolve. __resolve will be called if you attempt to call a function or reference a variable that doesnt exist.

myObj = new Object();
myObj.__resolve = function(identifier){
trace(identifier +" was accessed but does not exist");
}

a = myObj.myvar; // myvar was accessed but does not exist
myObj.mymeth(); // mymeth was accessed but does not exist
This, however, only works on the identifier referencing level (variables or functions). This means that its use is before arguments to a function are applied. Anything returned by the call to __resolve will then be run as a function and have those arguments applied on it.

function lastresort(){
trace("last resort using "+ arguments);
}
myObj = new Object();
myObj.__resolve = function(identifier){
trace(identifier +" was accessed but does not exist");
return lastresort;
}

myObj.mymeth(1,2,3);
// mymeth was accessed but does not exist
// last resort using 1,2,3

MaKa
March 1st, 2004, 10:30 AM
Thanks, I found this too, very nice.
http://chattyfig.figleaf.com/flashcoders-wiki/index.php?__resolve