__call function in AS3 ?

eelcol used Ask the Experts™

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


Eelco Luurtsema
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

No, that's not possible in ActionScript...



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 ?

You can catch the error by wrapping it in try ... catch block, but you cannot call the function that generated the error...

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Actually, (if I understand "__call" correctly) it is possible if that class extends the ActionScript Proxy class.


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,
Otherwise, if you can check if a function exists by doing this:

if (myClass["myFunction"])
   {  myClass.myFunction(); }


if (myClass["myFunction"] is Function)   { myClass.myFunction(); }

__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.

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?


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 :

clientObject.call(methodName, [resultObj, [p1, ... pN]])

- 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 ?


So I am still searching for a solution for this.

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.
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.



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 ?

Thanks !
Could you elaborate more on what you did, perhaps including some sample code. I don't quite understand what you mean from your description.

I'm quite curious how you got it solved, and might have some use for the solution in the future.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial