Loading Classes Dynamically

Alright, I’m in a major pickle here.

I’m developing a MySQL->PHP->Flash blog/CMS that I’m hoping to open source somewhere down the road. The problem I’m facing is that I want the admin to choose what kind of page each page is, and add more templates down the road.

So say the default pages it comes with are Staticpage and Blog. The admin sets the homepage to Staticpage and the blog page to Blog. This enters the information into the pageType value in the MySQL database. I then load this information into Flash. Up to here, it works perfectly, and it would work perfectly if I didn’t want the admins to have the ability to add their own pages.

I need to work out some sort of system where users can upload new Page files (AS files placed in the /pages/ directory) and Page Class files (AS files placed in the /classes/ directory) and have Flash recognize that these files are here, and access the code when a page of that type is being loaded.

[thread=216159]Apparently, I can’t dynamically include the files using variables[/thread] which would have worked perfectly.

Does anyone have ANY idea how I can dynamically access this code? TXT files? Anything?

I’ve noticed a few people that are dynamically changing generic Classes via XML. Basically they create / import / compile a dynamic class thats fairly empty - this may be your generic “page” class. Since its dynamic, they can add methods / props etc during runtime via XML to stucture the Class how they’d like, then create an instance of the class etc etc. Does this make sense? I’m sorry, I don’t have a link to any examples, but maybe this info may lead you to some solution - I’m continuing to dig for some examples, if I find, I’ll post.

Thanks for the reply creatify…

I’ve been digging around using Google, and I turned up this link. Read the bottom of the code section right there:


var myClassName:String = "com.foo.Bar";
var clasz:Class = getClassByName(myClassName);
var barInstance:Bar = new clasz();

Seems to be the thing I’m looking for. Unfortunately, I don’t think this is available in Actionscript 2. Shame.

Sorry, classes are compiled with the FLA as well :slight_smile:

Interesting - If you need to dynamically assign a Class to a movieclip - because you can’t assign it at compile time to an instance of the movieclip in the library you can assign it this way:

myMovieClip.proto = new myClassName();

hope this helps…

So basically I’m up a freaking creek until Actionscript 3 comes along?

whimper

Ohhhhhh getClassByName(), how I want you. :frowning:

Ok, well I’m partially an idiot.

Packages solved one half of the problem. The way I had my directory set up, I had DIRECTORY/core/classes, and then each individual Class file inside there. The way I have it now, it’s DIRECTORY/core/classes/pages/ and then have the Page class files in there. This way, I have the SWF import pages.*

The second half of the problem was a little more trouble. I searched around the default Classes directory for Flash, hoping to find something hidden that wasn’t documented. I searched for “Class” and it turned up a lovely little Class file under the name of “Classfinder.as”. This class allows you to get a class’ constructor function from a variable:


var nameOfClass:String = "pages.Testpage";
var classString:Function = mx.utils.ClassFinder.findClass(nameOfClass);
var page = new classString();

The problem with this code is that you need to already have used the class for it to work… so while I still have problems to deal with, I’m further along than I was before.

What I think I’ll do is rewrite the ClassFinder class to initiate and then delete an instance of the found class before it passes it back to the page variable so I don’t need to worry about anything else. I’ll keep you posted on how it goes :slight_smile:

Great find - keep us informed w/ how it goes…

Update:

Couldn’t get it working, so I’ll detail the system I have now so that someone searching can have a good idea of what works and what doesn’t.

In the parent directory, let’s call it flash, I have a file called initialize classes. Page authors need to write a file, called page-Classname.as and save it to /flash/pages/. This file has something along the lines of:


var display = new [insertClassName](0);
delete display;

initializeClasses.as links to all the files in /flash/pages/. Right now, my initializeClasses.as page has this:


#include "pages/page-Blog.as"
#include "pages/page-Staticpage.as"
#include "pages/page-Useradmin.as"

Anyway, once I run initializeClasses.as, all I need to do is use the code above to run the class from the variable pulled from the MySQL database. I have a feeling I’ll be able to write a code that gets all the file names from /flash/pages/ and dynamically generate the initializeClasses.as so that the user won’t have to do anything but upload the page’s files and go on with life.

This system so far has worked beyond beautifully for me, so I’m quite pleased with this.