Cloning Objects
Cloning Objects
To clone an object, call the %ConstructClone() method of that object. This method creates a new OREF.
The following Terminal session demonstrates this:
MYNAMESPACE>set person=##class(Sample.Person).%OpenId(1)
MYNAMESPACE>set NewPerson=person.%ConstructClone()
MYNAMESPACE>w
NewPerson=<OBJECT REFERENCE>[2@Sample.Person]
person=<OBJECT REFERENCE>[1@Sample.Person]
MYNAMESPACE>
Here, you can see that the NewPerson variable uses a different OREF than the original person object. NewPerson is a clone of person (or more precisely, these variables are pointers to separate but identical objects).
In contrast, consider the following Terminal session:
MYNAMESPACE>set person=##class(Sample.Person).%OpenId(1)
MYNAMESPACE>set NotNew=person
MYNAMESPACE>w
NotNew=<OBJECT REFERENCE>[1@Sample.Person]
person=<OBJECT REFERENCE>[1@Sample.Person]
Notice that here, both variables refer to the same OREF. That is, NotNew is not a clone of person.
For information on arguments to this method, see the InterSystems Class Reference for %Library.RegisteredObjectOpens in a new tab.