Copying typed objects

Attached below is a utility class with 3 static methods:

  • clone
  • compare
  • mergeObjects

Clone lets you copy Objects and Arrays. If you copy a different class, the public properties (and recursive public properties) will get transferred, but the private properties won't. Also, the cloned object can only be of type Object or Array.

Compare lets you compare recursively the public properties of two objects or arrays. Useful for multi-dimensional arrays.

MergeObjects takes two arguments, objectA and objectB. MergeObjects will take all public properties that are non-null on objectB, and copy them to objectA. This method does work with public getters/setters, and this method is not recursive.

If you need to do something more custom, the best approach would be to create an IClonable interface which has the method clone():* and create a custom clone method on your classes.
Note: private properties are private to the class itself, not the instance of that class. So this example works:

public class CustomClass implements IClonable {
    private var foo:String;
    public function clone():* {
        var bar:CustomClass = new CustomClass();
        bar.foo = foo;
        return bar;
    }
}

AttachmentSize
ObjectUtils.as1.78 KB

I can not find the attached class.

As for your clone method. You can clone other typed objects (only public properties) if you use a ByteArray in combination with register class alias.

Greetz Erik

Strange, you're right, it doesn't show up when I'm not logged in. It seems to be a bug with Drupal. I'll fix that when I get back from vacation tonight.