Is there a __call function in AS3, like in PHP ? I have a class which calls functions dynamicly and I want to catch these function calls in a __call function but I can't find anything about this. Is this possible
TIA
Eelco Luurtsema
Web DevelopmentAdobe Flash
Last Comment
IqAndreas
8/22/2022 - Mon
CyanBlue
No, that's not possible in ActionScript...
CyanBlue
eelcol
ASKER
I get the following error:
ReferenceError: Error #1069
Isn't it possible to catch this error, and then to call the function that generates the error (the one which doesn't exists) on another class ?
CyanBlue
You can catch the error by wrapping it in try ... catch block, but you cannot call the function that generated the error...
Whenever a property or function is being called or accessed, the "getProperty", "setProperty", and "callProperty" functions are run, and you can capture this.
Do you understand what it does? And I may have mistaken what __call does. I read the PHP reference on it, but I may have misunderstood, and if so, could you elaborate more on what the function does in PHP?
Good luck with your programming,
Andreas
IqAndreas
Otherwise, if you can check if a function exists by doing this:
if (myClass["myFunction"])
{ myClass.myFunction(); }
OR
if (myClass["myFunction"] is Function) { myClass.myFunction(); }
CyanBlue
__call() function in a nutshell is that you execute that function when your run a function that does not exist in a given class... For example, Animal class has walk, talk and eat methods, but you were trying animalInstance.sleep() where the __call function will kick in since there is no sleep method defined in the Animal class...
The Proxy class is close, but I don't think it is quite the same thing... You could extend the Proxy class and build your own, but I think that's quite extensive work you are looking...
Why not use Interface??? The Interface class basically is an agreement between the class provider and the developer where it tells you what you can and what you cannot... As an author of a class that you are using, it does not make sense to cover the case when your user is calling something that you did not allowed them for... That's my 2 cents...
Well a NetConnection instance from my Flash Media Server calls a function in my class, but that function may not exists in that class. Thats what I need it for.
I am going to look at the callProperty.
alien109
can you use more of an event listener pattern, and rather than attempting to call the method on the object itself have that object listen for events broadcasted by the instance?
eelcol
ASKER
I am looking at the option IqAndreas posted and it gets close.
The situation:
- From the Flash Media Server I call a function on the Actionscript on the user-side. I do this using the NetConnection Client.call() function. The documentation says that the function is :
- Strangely enough if I extend the Proxy class and have a callProperty, getProperty and setProperty function, the getProperty gets called when NetConnection calls a method ? In that way I lose the arguments.
So the problem seems to be that the Flash Media Server tries to retrieve an variabel (getProperty) while it should call a function ?
I am trying to build an own NetConnection class and thats where I get this problems. It is comparable with writing an own database class in PHP.
IqAndreas
Hey, sorry for the lack of response. I have been out and about traveling.
Do you think you could post the code you tried using with the Proxy class? I have an idea on how you can still keep the parameters even it runs the function "getProperty" instead, but it all depends on how your code is set up right now.
Other than the lack of parameter passing, does the code work as planned?
Sadly, I am unable to open FLA files in Linux, so plain text files with the code (or flex projects) would be best.
Cheers,
Andreas
eelcol
ASKER
Hi Andreas
I have managed to make a work-around for this with a kind of "catch-all" function.
All requests from Flash media server with NetConnection to a function, are being send to this function. This function then "redirects" the request to the right function in the parent class.
Is this a good solution or do you think you have a better one ?
CyanBlue