# Loading Classes Dynamically

**URL:** <https://forum.kirupa.com/t/loading-classes-dynamically/185915>\
**Category:** flash\
**Created:** [April 17, 2006, 5:39pm UTC](https://forum.kirupa.com/t/loading-classes-dynamically/185915 "2006-04-17T17:39:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![McGuffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mcguffin/32/408_2.png) [@McGuffin](https://forum.kirupa.com/u/McGuffin)\
**Post date:** [April 17, 2006, 5:39pm UTC](https://forum.kirupa.com/t/loading-classes-dynamically/185915/1 "2006-04-17T17:39:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [April 17, 2006, 5:53pm UTC](https://forum.kirupa.com/t/loading-classes-dynamically/185915/2 "2006-04-17T17:53:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![McGuffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mcguffin/32/408_2.png) [@McGuffin](https://forum.kirupa.com/u/McGuffin)\
**Post date:** [April 17, 2006, 6:09pm UTC](https://forum.kirupa.com/t/loading-classes-dynamically/185915/3 "2006-04-17T18:09:28Z")

</div>

Thanks for the reply creatify…

I’ve been digging around using Google, and I turned up [this link](http://labs.macromedia.com/wiki/index.php/ActionScript_3:resources:FAQ#How_can_I_get_meta_information_about_a_class_instance.3F). Read the bottom of the code section right there:

```auto

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.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [April 17, 2006, 6:14pm UTC](https://forum.kirupa.com/t/loading-classes-dynamically/185915/4 "2006-04-17T18:14:21Z")

</div>

Sorry, classes are compiled with the FLA as well 🙂

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [April 17, 2006, 6:14pm UTC](https://forum.kirupa.com/t/loading-classes-dynamically/185915/5 "2006-04-17T18:14:42Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![McGuffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mcguffin/32/408_2.png) [@McGuffin](https://forum.kirupa.com/u/McGuffin)\
**Post date:** [April 17, 2006, 6:18pm UTC](https://forum.kirupa.com/t/loading-classes-dynamically/185915/6 "2006-04-17T18:18:15Z")

</div>

> [@TheCanadian](#):
>
> Sorry, classes are compiled with the FLA as well 🙂

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

[_whimper_](http://livedocs.macromedia.com/labs/1/flex20beta2/langref//Class.html)

Ohhhhhh getClassByName(), how I want you. ☹

---

<div class="post-metadata">

**Author:** ![McGuffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mcguffin/32/408_2.png) [@McGuffin](https://forum.kirupa.com/u/McGuffin)\
**Post date:** [April 17, 2006, 10:40pm UTC](https://forum.kirupa.com/t/loading-classes-dynamically/185915/7 "2006-04-17T22:40:36Z")

</div>

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:

```auto

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 🙂

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [April 18, 2006, 3:27pm UTC](https://forum.kirupa.com/t/loading-classes-dynamically/185915/8 "2006-04-18T15:27:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![McGuffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mcguffin/32/408_2.png) [@McGuffin](https://forum.kirupa.com/u/McGuffin)\
**Post date:** [April 19, 2006, 1:07am UTC](https://forum.kirupa.com/t/loading-classes-dynamically/185915/9 "2006-04-19T01:07:27Z")

</div>

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:

```auto

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:

```auto

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